sql 遍历所有表中 某项 值为已知数的查询语句

USE [C6]
GO
/****** 对象:  StoredProcedure [dbo].[Full_Search]    脚本日期: 08/11/2011 09:35:43 tzjly******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER proc [dbo].[Full_Search](@string varchar(50))
as
begin

declare @tbname varchar(50)
declare tbroy cursor for select name from sysobjects
where xtype= 'u ' --第一个游标遍历所有的表

open tbroy
fetch next from tbroy into @tbname
while @@fetch_status=0
begin

declare @colname varchar(50)
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 ') --数据类型为字符型的字段
) --第二个游标是第一个游标的嵌套游标,遍历某个表的所有字段

open colroy
fetch next from colroy into @colname
while @@fetch_status=0
begin

declare @sql nvarchar(1000),@j int
select @sql= 'select @i=count(1) from ' +@tbname + ' where '+ @colname+ ' like '+ '''%'+@string+ '%'''
exec sp_executesql @sql,N'@i int output',@i=@j output --输出满足条件表的记录数
if @j> 0
BEGIN
select 包含字串的表名=@tbname
--exec( 'select distinct '+@colname+' from ' +@tbname + ' where '+ @colname+ ' like '+ '''%'+@string+ '%''')
END
fetch next from colroy into @colname
end

close colroy
deallocate colroy

fetch next from tbroy into @tbname
end
close tbroy
deallocate tbroy
end

exec Full_Search '2011-08-16'

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值