我是靠谱客的博主 糊涂豆芽,这篇文章主要介绍sql遍历所有表中某项值为已知数的查询方法,现在分享给大家,希望可以做个参考。

下面将为您介绍sql遍历所有表中某项值为已知数的查询语句写法,供您参考,如果您对sql遍历方面感兴趣的话,不妨一看,希望对您有所帮助。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
CREATE proc 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 go exec Full_Search '123'

以上就是sql遍历所有表中某项值为已知数的查询方法。


最后

以上就是糊涂豆芽最近收集整理的关于sql遍历所有表中某项值为已知数的查询方法的全部内容,更多相关sql遍历所有表中某项值为已知数内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(90)

评论列表共有 0 条评论

立即
投稿
返回
顶部