当前位置:   article > 正文

sqlserver全库搜索关键字

sqlserver全库搜索关键字
  1. declare @Str nvarchar(max), @tableName varchar(50), @colName varchar(50), @rowCount int
  2. select a.name tableName, b.name Colname, 0 as IsFound into #t1
  3. from sysobjects a join syscolumns b on a.id=b.id join systypes c on b.xtype=c.xtype
  4. where a.[type]='U' and c.name in ('varchar', 'nvarchar', 'char', 'nchar') --这里是设置字段的类型,以缩小范围
  5. declare _c1 cursor for select Colname, tableName from #t1
  6. open _c1
  7. fetch next from _c1 into @colName, @tableName
  8. while @@FETCH_STATUS=0 begin
  9. --print @Str
  10. select @Str='select @rowCount=count(1) from ['+@tableName+'] where ['+@colName+'] like ''%keywords%''' --这里是要查找的内容
  11. exec sp_executesql @Str, N'@rowCount int output', @rowCount output
  12. if @rowCount>0 update #t1 set IsFound=1 where ColName=@colName and tableName=@tableName
  13. fetch next from _c1 into @colName, @tableName
  14. end
  15. close _c1
  16. deallocate _c1
  17. select * from #t1 where IsFound=1
  18. drop table #t1

tableName:表名
Colname:列名
IsFound:找到的个数

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/168579
推荐阅读
相关标签
  

闽ICP备14008679号