在整个SQL SERVER数据库的所有表中查找一个字符(比如姓名)

select convert(varchar(255),'') dsca
into #y
where 1=0

-- delete #y
declare @s varchar(255)
set @s='也许疯'
DECLARE bbb cursor for
select  TABLE_NAME,column_name  FROM information_schema.columns
    where table_name in (select  table_name from information_schema.tables where TABLE_type='BASE TABLE' and table_name like '%%' )
    and data_type like '%varchar%'


declare @t varchar(255)
declare @f varchar(255)
open bbb
fetch  next from bbb into @t,@f
while @@fetch_status=0
begin

exec( ' if exists (select * from '+@t+' where '+@f+'='+''''+@s+''''+'  )  insert into #y select '+''''+@t+'.'+@f+''''  )


--insert into #t select  '+''''+'select * from  '+@t+' WHERE '+@f+'=@F'+'''

fetch  next from bbb into @t,@f

end
close bbb
deallocate  bbb

select * from #y
drop table #y

你可能感兴趣的:(数据库开发)