以前写过类似的,参考一下:
declare @str nvarchar(20)
declare @tablename varchar(50)
declare @colname varchar(50)
declare @counts int
declare @sql nvarchar(2000)--以上定义变量
declare cur1 cursor for
select a.name tablename,B.name colname from sys.objects a,syscolumns b where a.object_id=b.id and a.type_desc='USER_TABLE'--定义游标
set @str='CT无袖开胸上衣' --设置要查找的字符
Open cur1 --打开游标
Fetch next From cur1 Into @tablename,@colname
While(@@Fetch_Status = 0) --循环
Begin
set @sql=N'select @counts=COUNT(*) from ' + @tablename + ' where charindex('''+@str+''','+@colname+')>0'
exec sp_executesql @sql,N'@counts int output',@counts output --执行动态sql
if @counts>0 --判断某个表某个字段是否存在特定字符
begin
print @tablename+','+@colname --打印表名及字段名
end
Fetch next From cur1 Into @tablename,@colname
End
Close cur1 --关闭游标
Deallocate cur1 --释放游标