查看: 1797|回复: 0

[文章教程] mssql sqlserver 整个库搜索字符串

[复制链接]
xuanxiao 发表于 2018-10-11 10:15:27 | 显示全部楼层 |阅读模式
  1. mssql sqlserver 整个库搜索字符串


  2. if   exists   (select   *   from   dbo.sysobjects   where   id   =   object_id(N'[dbo].[p_search]')   and   OBJECTPROPERTY(id,   N'IsProcedure')   =   1)  
  3.   drop   procedure   [dbo].[p_search]  
  4.   GO  
  5.    
  6.   /*--搜索某个字符串在那个表的那个字段中  
  7.   p_search 'aaaa'
  8.   --邹建   2004.10(引用请保留此信息)--*/  
  9.    
  10.   /*--调用示例  
  11.   use   pubs  
  12.   exec   p_search   N'l'  
  13.   --*/  
  14.   create   proc   p_search  
  15.   @str   Nvarchar(1000)   --要搜索的字符串  
  16.   as  
  17.   if   @str   is   null   return  
  18.    
  19.   declare   @s   Nvarchar(4000)  
  20.   create   table   #t(表名   sysname,字段名   sysname)  
  21.    
  22.   declare   tb   cursor   local   for  
  23.   select   s='if   exists(select   1   from   ['+replace(b.name,']',']]')+']   where   ['+a.name+']   like   N''%'+@str+'%'')  
  24.   print   ''所在的表及字段:   ['+b.name+'].['+a.name+']'''  
  25.   from   syscolumns   a   join   sysobjects   b   on   a.id=b.id  
  26.   where   b.xtype='U'   and   a.status>=0  
  27.   and   a.xtype   in(175,239,99,35,231,167)  
  28.   open   tb  
  29.   fetch   next   from   tb   into   @s  
  30.   while   @@fetch_status=0  
  31.   begin  
  32.   exec(@s)  
  33.   fetch   next   from   tb   into   @s  
  34.   end  
  35.   close   tb  
  36.   deallocate   tb  
  37.   go
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表

在线客服

售前咨询
售后咨询
服务热线
023-58418553
微信公众号