网上现有的全库查询,无法识别自定义架构的数据库结构:
declare @str nvarchar(10) 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=‘海‘ --设置要查找的字符 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 --释放游标
添加架构支持后,虽偶尔会有报错,可查询架构:
declare @str nvarchar(10) declare @schemasname varchar(50) declare @tablename varchar(50) declare @colname varchar(50) declare @counts int declare @sql nvarchar(2000)--以上定义变量 declare cur1 cursor for select s.name schemasname,a.name tablename,B.name colname from sys.objects a,syscolumns b,sys.schemas s where a.object_id=b.id and a.schema_id=s.schema_id and a.type_desc=‘USER_TABLE‘--定义游标 set @str=‘污染‘ --设置要查找的字符 Open cur1 --打开游标 Fetch next From cur1 Into @schemasname,@tablename,@colname While(@@Fetch_Status = 0) --循环 Begin set @sql=N‘select @counts=COUNT(*) from ‘ + @schemasname + ‘.‘+ @tablename + ‘ where charindex(‘‘‘+@str+‘‘‘,‘+@colname+‘)>0‘ exec sp_executesql @sql,N‘@counts int output‘,@counts output --执行动态sql if @counts>0 --判断某个表某个字段是否存在特定字符 begin print @schemasname+‘,‘+@tablename+‘,‘+@colname --打印表名及字段名 end Fetch next From cur1 Into @schemasname,@tablename,@colname End Close cur1 --关闭游标 Deallocate cur1 --释放游标
时间: 2024-10-26 02:46:29