SQL Server在数据库中查找指定字符串并定位表名

SqlServer,只有一条信息如何查找到底在哪个表里呢~如下:

declare @key varchar(30) 
set @key = 'admin' --要查找的字符串 
declare @tabName varchar(40),@colName varchar(40) 
declare @sql varchar(2000) 
declare @tsql varchar(8000) 
declare tabCursor cursor for 
select name from sysobjects where xtype = 'u' AND name <> 'dtproperties' --查询所有用户表的名称 
open tabCursor 
fetch NEXT from tabCursor into @tabName 
while @@fetch_status = 0 
begin 
set @tsql = '' 
declare colCursor cursor for
select name from SysColumns where id=Object_Id(@tabName) and xtype=167 and length=50 --查询所有用户表的列名称
open colCursor 
fetch NEXT from colCursor into @colName 
while @@fetch_status = 0 
begin 
set @sql = 'if(exists(select * from ' + @tabName + ' where ' 
set @sql = @sql + @colName + ' like ''%' + @key + '%'')) begin select * from ' 
set @sql = @sql + @tabName + ' where ' + @colName + ' like ''%' + @key + '%'';select ''' 
+ @tabName + ''' as TableName end' 
set @tsql = @tsql + @sql + ';' 
fetch NEXT from colCursor into @colName 
end 
exec(@tsql) 
close colCursor 
deallocate colCursor 
fetch NEXT from tabCursor into @tabName 
end 
close tabCursor 
deallocate tabCursor

结果如下图:

20132230841390

上一篇
下一篇