如果是指的列名的话,很简单,将 like 后替代为你所需要查找的列名即可
select sysobjects.name as tablename,syscolumns.name as columnname
from syscolumns join sysobjects on sysobjects.id = syscolumns.id
where syscolumns.name like '%此处为列名%'
如果是指的一个值的话,这种情况相对要复杂一点,需要遍历数据库。如下:
declare@str varchar(100)
set@str='张三'--要搜索的字符串
declare@s varchar(8000)
declaretb cursorlocalfor
selects='if exists(select 1 from ['+b.name+'] where convert(varchar,['+a.name+']) like ''%'+@str+'%'')
print ''select ['+a.name+'] from ['+b.name+']'''
fromsyscolumns a joinsysobjects b ona.id=b.id
whereb.xtype='U'anda.status>=0
--所查列的字段类型
anda.xusertype in(175,239,231,167,56,60,108,106)
opentb
fetchnextfromtb into@s
while @@fetch_status=0
begin
exec(@s)
fetchnextfromtb into@s
end
closetb
deallocatetb