SQL Server 全库筛选指定字符串操作记录
过程描述:
- 获取所有表和字段:只要字符型字段,字符串一定是字符型,如果要查询其它的就按需选择字段,避免字段类型错误;
- 通过excel拼接生成sql:每个字段一条查询语句。
- 查询结果union all 拼接到一起。
获取所有表和字段名
SELECT t.name,c.name
FROM SYSOBJECTS t,SYSCOLUMNS c
WHERE t.xtype= 'U' AND t.id= c.id AND c.xusertype IN (
SELECT xusertype FROM SYSTYPES WHERE name IN ( 'char', 'varchar', 'text', 'nchar', 'nvarcha', 'ntext' ))
说明:只要字符型字段
导出到excel公式拼接sql语句
="select '"&A2&"' as tt,'"&B2&"' as cc,"&B2&" as jg from "&A2 &" where "&B2 &" like '%指定字符串%' union all"
拿几行测试
select 'cms_archive' as tt,'title' as cc,title as jg from cms_archive where title like '%2022%' union all
select 'cms_archive' as tt,'subtitle' as cc,subtitle as jg from cms_archive where subtitle like '%2022%' union all
select 'cms_archive' as tt,'source' as cc,source as jg from cms_archive where source like '%2022%' union all
select 'cms_archive' as tt,'author' as cc,author as jg from cms_archive where author like '%2022%'
执行全部
执行可能会比较慢,需要多等一会。