新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
sql遍歷所有表中某項值為已知數(shù)的查詢方法
下面將為您介紹sql遍歷所有表中某項值為已知數(shù)的查詢語句寫法,供您參考,如果您對sql遍歷方面感興趣的話,不妨一看,希望對您有所幫助。

成都創(chuàng)新互聯(lián)公司專注于湯原企業(yè)網(wǎng)站建設(shè),響應(yīng)式網(wǎng)站建設(shè),成都做商城網(wǎng)站。湯原網(wǎng)站建設(shè)公司,為湯原等地區(qū)提供建站服務(wù)。全流程定制網(wǎng)站制作,專業(yè)設(shè)計,全程項目跟蹤,成都創(chuàng)新互聯(lián)公司專業(yè)和態(tài)度為您提供的服務(wù)
- 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 ') --數(shù)據(jù)類型為字符型的字段
- ) --第二個游標是***個游標的嵌套游標,遍歷某個表的所有字段
- 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 --輸出滿足條件表的記錄數(shù)
- 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遍歷所有表中某項值為已知數(shù)的查詢方法。
【編輯推薦】
SQL Server安全解析
sql server安全的兩層模型
讓sqlserver恢復(fù)到某個時間點
使用Easy Recovery實現(xiàn)sql server恢復(fù)
SQL Server系統(tǒng)表的作用
網(wǎng)站題目:sql遍歷所有表中某項值為已知數(shù)的查詢方法
網(wǎng)站鏈接:http://m.5511xx.com/article/dppddse.html


咨詢
建站咨詢
