1. if exists (select * from sysobjects where name = ‘Full_Search’)  
  2.   drop proc Full_Search  
  3. go  
  4. create proc Full_Search(@old_string varchar(20))  
  5. as  
  6. begin  
  7. declare @tbname varchar(50)  
  8. declare tbroy cursor for select name from sysobjects where xtype= ‘U ‘ –第一个游标遍历所有的表  
  9. open tbroy  
  10. fetch next from tbroy into @tbname  
  11. while @@fetch_status=0  
  12. begin  
  13. declare @colname varchar(50)  
  14. declare colroy cursor for select name from syscolumns where id=object_id(@tbname) and xtype in (select xtype from systypes where name in ( ‘varchar‘, ‘nvarchar’, ‘char‘, ‘nchar‘, ‘text’)) –第二个游标是第一个游标的嵌套游标,遍历某个表的所有字段  
  15. open colroy  
  16. fetch next from colroy into @colname  
  17. while @@fetch_status=0  
  18. begin  
  19. declare @sql nvarchar(1000),@j int  
  20. select @sql= ‘select @i=count(1) from ‘ + ‘[‘ + @tbname + ‘]’ + ‘ where ‘+ ‘[‘ + @colname + ‘]’+ ‘ like ‘+ ‘%’+ @old_string + ‘%‘  
  21. –select @sql  
  22. exec dbo.sp_executesql @sql,N’@i int output‘, @i = @j output –输出满足条件表的记录数  
  23. if @j> 0  
  24. begin  
  25. select 包含字串的表名 = @tbname ,包含字串的列名 = @colname  
  26. –select @sql = ‘select ‘ + @colname + ‘,’ + ‘ from ‘ + @tbname + ‘ where ‘+ @colname + ‘ like ‘+ ”’%’ + @old_string + ‘%”’  
  27. –select @sql  
  28. –exec(@sql)  
  29. end  
  30. fetch next from colroy into @colname  
  31. end  
  32. close colroy  
  33. deallocate colroy  
  34. fetch next from tbroy into @tbname  
  35. end  
  36. close tbroy  
  37. deallocate tbroy  
  38. end  
  39. go  
  40. exec Full_Search ‘韦贝贝’ –需要查找的内容  
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系贝贝进行处理。
本站默认解压密码:www.hibbba.com