亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

sql存儲過程全站搜索、并分頁

sql存儲過程全站搜索、并分頁

阿波羅的戰車 2018-12-07 08:55:14
--但只已知一個字符串,去查詢整個數據庫內有那些表里面包含這個字符串,用游標可以實現了這個需求,但是我想求一個能查詢數據庫里面所有nvarchar、varchar并且能分頁的存儲過程:?? create?proc?Full_Search(@string?varchar(50))asbegin?declare?@tbname?varchar(50)?declare?tbroy?cursor??for?select?name?from?sysobjects??where?xtype='u'???--第一個游標遍歷所有的表?open?tbroyfetch?next?from?tbroy?into?@tbnamewhile?@@fetch_status=0begin????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?exec('select??distinct?'+@colname+'?from?'?+@tbname?+'??where?'+?@colname+'?like?'+'''%'+@string+'%''')??fetch?next?from?colroy?into?@colname?end??????????close?colroy?deallocate?colroy?fetch?next?from?tbroy?into?@tbnameendclose?tbroydeallocate?tbroyend?exec?Full_Search??'市場'?drop?proc?Full_Search ? ?
查看完整描述

2 回答

?
DIEA

TA貢獻1820條經驗 獲得超3個贊

問題打錯了~

查看完整回答
反對 回復 2019-01-07
?
搖曳的薔薇

TA貢獻1793條經驗 獲得超6個贊

建議使用CTE(Common Table Expressions,公用表表達式),參考:SQL Server2005的幾種分頁方法

查看完整回答
反對 回復 2019-01-07
  • 2 回答
  • 0 關注
  • 618 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號