新聞中心
這里有您想知道的互聯(lián)網營銷解決方案
教您使用SQL游標解決查詢全表問題
借助SQL游標,可以輕松解決SQL查詢全表的問題。下面就為您介紹該SQL游標的寫法,供您參考,希望對您學習SQL游標有所幫助。

梅州網站建設公司創(chuàng)新互聯(lián)建站,梅州網站設計制作,有大型網站制作公司豐富經驗。已為梅州成百上千提供企業(yè)網站建設服務。企業(yè)網站搭建\外貿網站制作要多少錢,請找那個售后服務好的梅州做網站的公司定做!
- Create procedure Full_Search(@string varchar(100))
- as
- begin
- declare @tbname varchar(100)
- 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(100)
- 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(4000),@j int
- select @sql='select @i=count(1) from ' +quotename(@tbname) +' where '+ quotename(@colname)+' like '+'''%'+@string+'%'''
- exec sp_executesql @sql,N'@i int output',@i=@j output --輸出滿足條件表的記錄數
- if @j>0
- begin
- declare @v varchar(8000)
- set @v='select distinct '+quotename(@colname)+' from ' +quotename(@tbname) +' where '+ quotename(@colname)+' like '+'''%'+@string+'%'''
- exec(@v)
- 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 '市場'
- drop proc Full_Search
【編輯推薦】
如何給SQL查詢添加合計行
巧用sql替換語句
SQL替換語句語法說明
sql條件語句使用實例
SQL Where子句的妙用
文章標題:教您使用SQL游標解決查詢全表問題
文章地址:http://www.5511xx.com/article/coscgjh.html


咨詢
建站咨詢
