这个存储过程查询数据库中的所有的表是否包含要查询的字符串

--下面这个存储过程用来在某个数据库中的所有表中查找某一字符串
----使用: EXEC Search '张三'
CREATE PROCEDURE Search @Str varchar(100),@SearchFlag  int=1,@TableFlag int=1
/**
**@Str 要搜索的字符串
**@TableFlag 1: 只在用户表中查找;2:只在系统表中查找;其他:在所有表中查找
**@SearchFlag 1: 精确查询;其他:模糊查询
**/
As
begin
CREATE table #TableList(tablename sysname,colname sysname)
declare @table sysname
declare @col sysname
set nocount on
if @TableFlag=1
  declare curTab scroll cursor for select name from sysobjects where xtype='U' and status>0
else
  if @TableFlag=2
    declare curTab scroll cursor for select name from sysobjects where xtype='S'
  else
     declare curTab scroll cursor for select name from sysobjects where xtype='S' or xtype='U'
open curTab
fetch next from curTab into @table
while @@FETCH_STATUS=0
begin
  declare curCol scroll cursor for select name from syscolumns where (xtype=175 or xtype=167 or xtype=239 or xtype=231) and (id in (select id from sysobjects where name=@table))
  open curCol
  fetch next from curCol into @col
  while @@FETCH_STATUS=0
  begin
    if @SearchFlag=1
       execute('insert into #TableList select '''+@table+''','''+@col+''' from '+@table+' where '''+@table+''','''+@col+''' from '+@table+' where '+@col+' like '''+ '%'+@str+ '%'+'''')
    fetch next from curCol into @col
  end
  close curCol
  deallocate curCol
  fetch next from curTab into @table
end
close curTab
deallocate curTab
set nocount off
select  distinct * from #TableList
drop table #tablelist 
end
GO
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值