日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

[转]Sql Server 分页存储过程

發布時間:2024/4/14 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [转]Sql Server 分页存储过程 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

本文轉自:

分頁存儲過程一:

?

[sql] view plaincopy print?
  • --/*-----存儲過程?分頁處理?孫偉?2005-03-28創建?-------*/???
  • --/*-----?對數據進行了2分處理使查詢前半部分數據與查詢后半部分數據性能相同?-------*/???
  • --/*-----存儲過程?分頁處理?孫偉?2005-04-21修改?添加Distinct查詢功能-------*/???
  • --/*-----存儲過程?分頁處理?孫偉?2005-05-18修改?多字段排序規則問題-------*/???
  • --/*-----存儲過程?分頁處理?孫偉?2005-06-15修改?多字段排序修改-------*/???
  • --/*-----存儲過程?分頁處理?孫偉?2005-12-13修改?修改數據分頁方式為top?max模式性能有極大提高-------*/???
  • --/*-----缺點:相對之前的not?in版本主鍵只能是整型字段,如主鍵為GUID類型請使用not?in?模式的版本-------*/???
  • CREATE?PROCEDURE?dbo.proc_ListPageInt???
  • (???
  • @tblName?nvarchar(200),?----要顯示的表或多個表的連接???
  • @fldName?nvarchar(500)?=?'*',?----要顯示的字段列表???
  • @pageSize?int?=?10,?----每頁顯示的記錄個數???
  • @page?int?=?1,?----要顯示那一頁的記錄???
  • @pageCount?int?=?1?output,?----查詢結果分頁后的總頁數???
  • @Counts?int?=?1?output,?----查詢到的記錄數???
  • @fldSort?nvarchar(200)?=?null,?----排序字段列表或條件???
  • @Sort?bit?=?0,?----排序方法,0為升序,1為降序(如果是多字段排列Sort指代最后一個排序字段的排列順序(最后一個排序字段不加排序標記)--程序傳參如:'?SortA?Asc,SortB?Desc,SortC?')???
  • @strCondition?nvarchar(1000)?=?null,?----查詢條件,不需where???
  • @ID?nvarchar(150),?----主表的主鍵???
  • @Dist?bit?=?0?----是否添加查詢字段的?DISTINCT?默認0不添加/1添加???
  • )???
  • AS???
  • SET?NOCOUNT?ON???
  • Declare?@sqlTmp?nvarchar(1000)?----存放動態生成的SQL語句???
  • Declare?@strTmp?nvarchar(1000)?----存放取得查詢結果總數的查詢語句???
  • Declare?@strID?nvarchar(1000)?----存放取得查詢開頭或結尾ID的查詢語句???
  • ??
  • Declare?@strSortType?nvarchar(10)?----數據排序規則A???
  • Declare?@strFSortType?nvarchar(10)?----數據排序規則B???
  • ??
  • Declare?@SqlSelect?nvarchar(50)?----對含有DISTINCT的查詢進行SQL構造???
  • Declare?@SqlCounts?nvarchar(50)?----對含有DISTINCT的總數查詢進行SQL構造???
  • ??
  • ??
  • if?@Dist?=?0???
  • begin???
  • set?@SqlSelect?=?'select?'???
  • set?@SqlCounts?=?'Count(*)'???
  • end???
  • else???
  • begin???
  • set?@SqlSelect?=?'select?distinct?'???
  • set?@SqlCounts?=?'Count(DISTINCT?'+@ID+')'???
  • end???
  • ??
  • ??
  • if?@Sort=0???
  • begin???
  • set?@strFSortType='?ASC?'???
  • set?@strSortType='?DESC?'???
  • end???
  • else???
  • begin???
  • set?@strFSortType='?DESC?'???
  • set?@strSortType='?ASC?'???
  • end???
  • ??
  • ??
  • ??
  • --------生成查詢語句--------???
  • --此處@strTmp為取得查詢結果數量的語句???
  • if?@strCondition?is?null?or?@strCondition=''?--沒有設置顯示條件???
  • begin???
  • set?@sqlTmp?=?@fldName?+?'?From?'?+?@tblName???
  • set?@strTmp?=?@SqlSelect+'?@Counts='+@SqlCounts+'?FROM?'+@tblName??
  • set?@strID?=?'?From?'?+?@tblName???
  • end???
  • else???
  • begin???
  • set?@sqlTmp?=?+?@fldName?+?'From?'?+?@tblName?+?'?where?(1>0)?'?+?@strCondition???
  • set?@strTmp?=?@SqlSelect+'?@Counts='+@SqlCounts+'?FROM?'+@tblName?+?'?where?(1>0)?'?+?@strCondition???
  • set?@strID?=?'?From?'?+?@tblName?+?'?where?(1>0)?'?+?@strCondition???
  • end???
  • ??
  • ----取得查詢結果總數量-----???
  • exec?sp_executesql?@strTmp,N'@Counts?int?out?',@Counts?out???
  • declare?@tmpCounts?int???
  • if?@Counts?=?0???
  • set?@tmpCounts?=?1???
  • else???
  • set?@tmpCounts?=?@Counts???
  • ??
  • --取得分頁總數???
  • set?@pageCount=(@tmpCounts+@pageSize-1)/@pageSize???
  • ??
  • /**//**當前頁大于總頁數?取最后一頁**/???
  • if?@page>@pageCount???
  • set?@page=@pageCount???
  • ??
  • --/*-----數據分頁2分處理-------*/???
  • declare?@pageIndex?int?--總數/頁大小???
  • declare?@lastcount?int?--總數%頁大小???
  • ??
  • set?@pageIndex?=?@tmpCounts/@pageSize???
  • set?@lastcount?=?@tmpCounts%@pageSize???
  • if?@lastcount?>?0???
  • set?@pageIndex?=?@pageIndex?+?1???
  • else???
  • set?@lastcount?=?@pagesize???
  • ??
  • --//***顯示分頁???
  • if?@strCondition?is?null?or?@strCondition=''?--沒有設置顯示條件???
  • begin???
  • if?@pageIndex<2?or?@page<=@pageIndex?/?2?+?@pageIndex?%?2?--前半部分數據處理???
  • begin???
  • if?@page=1???
  • set?@strTmp=@SqlSelect+'?top?'+?CAST(@pageSize?as?VARCHAR(4))+'?'+?@fldName+'?from?'+@tblName??
  • +'?order?by?'+?@fldSort?+'?'+?@strFSortType???
  • else???
  • begin???
  • set?@strTmp=@SqlSelect+'?top?'+?CAST(@pageSize?as?VARCHAR(4))+'?'+?@fldName+'?from?'+@tblName??
  • +'?where?'+@ID+'?<(select?min('+?@ID?+')?from?('+?@SqlSelect+'?top?'+?CAST(@pageSize*(@page-1)?as?Varchar(20))?+'?'+?@ID?+'?from?'+@tblName??
  • +'?order?by?'+?@fldSort?+'?'+?@strFSortType+')?AS?TBMinID)'???
  • +'?order?by?'+?@fldSort?+'?'+?@strFSortType???
  • end???
  • end???
  • else???
  • begin???
  • set?@page?=?@pageIndex-@page+1?--后半部分數據處理???
  • if?@page?<=?1?--最后一頁數據顯示???
  • set?@strTmp=@SqlSelect+'?*?from?('+@SqlSelect+'?top?'+?CAST(@lastcount?as?VARCHAR(4))+'?'+?@fldName+'?from?'+@tblName??
  • +'?order?by?'+?@fldSort?+'?'+?@strSortType+')?AS?TempTB'+'?order?by?'+?@fldSort?+'?'+?@strFSortType???
  • else???
  • set?@strTmp=@SqlSelect+'?*?from?('+@SqlSelect+'?top?'+?CAST(@pageSize?as?VARCHAR(4))+'?'+?@fldName+'?from?'+@tblName??
  • +'?where?'+@ID+'?>(select?max('+?@ID?+')?from('+?@SqlSelect+'?top?'+?CAST(@pageSize*(@page-2)+@lastcount?as?Varchar(20))?+'?'+?@ID?+'?from?'+@tblName??
  • +'?order?by?'+?@fldSort?+'?'+?@strSortType+')?AS?TBMaxID)'???
  • +'?order?by?'+?@fldSort?+'?'+?@strSortType+')?AS?TempTB'+'?order?by?'+?@fldSort?+'?'+?@strFSortType???
  • end???
  • end???
  • ??
  • else?--有查詢條件???
  • begin???
  • if?@pageIndex<2?or?@page<=@pageIndex?/?2?+?@pageIndex?%?2?--前半部分數據處理???
  • begin???
  • if?@page=1???
  • set?@strTmp=@SqlSelect+'?top?'+?CAST(@pageSize?as?VARCHAR(4))+'?'+?@fldName+'?from?'+@tblName??
  • +'?where?1=1?'?+?@strCondition?+?'?order?by?'+?@fldSort?+'?'+?@strFSortType???
  • else???
  • begin???
  • set?@strTmp=@SqlSelect+'?top?'+?CAST(@pageSize?as?VARCHAR(4))+'?'+?@fldName+'?from?'+@tblName??
  • +'?where?'+@ID+'?<(select?min('+?@ID?+')?from?('+?@SqlSelect+'?top?'+?CAST(@pageSize*(@page-1)?as?Varchar(20))?+'?'+?@ID?+'?from?'+@tblName??
  • +'?where?(1=1)?'?+?@strCondition?+'?order?by?'+?@fldSort?+'?'+?@strFSortType+')?AS?TBMinID)'???
  • +'?'+?@strCondition?+'?order?by?'+?@fldSort?+'?'+?@strFSortType???
  • end???
  • end???
  • else???
  • begin???
  • set?@page?=?@pageIndex-@page+1?--后半部分數據處理???
  • if?@page?<=?1?--最后一頁數據顯示???
  • set?@strTmp=@SqlSelect+'?*?from?('+@SqlSelect+'?top?'+?CAST(@lastcount?as?VARCHAR(4))+'?'+?@fldName+'?from?'+@tblName??
  • +'?where?(1=1)?'+?@strCondition?+'?order?by?'+?@fldSort?+'?'+?@strSortType+')?AS?TempTB'+'?order?by?'+?@fldSort?+'?'+?@strFSortType???
  • else???
  • set?@strTmp=@SqlSelect+'?*?from?('+@SqlSelect+'?top?'+?CAST(@pageSize?as?VARCHAR(4))+'?'+?@fldName+'?from?'+@tblName??
  • +'?where?'+@ID+'?>(select?max('+?@ID?+')?from('+?@SqlSelect+'?top?'+?CAST(@pageSize*(@page-2)+@lastcount?as?Varchar(20))?+'?'+?@ID?+'?from?'+@tblName??
  • +'?where?(1=1)?'+?@strCondition?+'?order?by?'+?@fldSort?+'?'+?@strSortType+')?AS?TBMaxID)'???
  • +'?'+?@strCondition+'?order?by?'+?@fldSort?+'?'+?@strSortType+')?AS?TempTB'+'?order?by?'+?@fldSort?+'?'+?@strFSortType???
  • end???
  • end???
  • ??
  • ------返回查詢結果-----???
  • exec?sp_executesql?@strTmp???
  • --print?@strTmp???
  • SET?NOCOUNT?OFF???
  • GO???
  • --/*-----存儲過程 分頁處理 孫偉 2005-03-28創建 -------*/ --/*----- 對數據進行了2分處理使查詢前半部分數據與查詢后半部分數據性能相同 -------*/ --/*-----存儲過程 分頁處理 孫偉 2005-04-21修改 添加Distinct查詢功能-------*/ --/*-----存儲過程 分頁處理 孫偉 2005-05-18修改 多字段排序規則問題-------*/ --/*-----存儲過程 分頁處理 孫偉 2005-06-15修改 多字段排序修改-------*/ --/*-----存儲過程 分頁處理 孫偉 2005-12-13修改 修改數據分頁方式為top max模式性能有極大提高-------*/ --/*-----缺點:相對之前的not in版本主鍵只能是整型字段,如主鍵為GUID類型請使用not in 模式的版本-------*/ CREATE PROCEDURE dbo.proc_ListPageInt ( @tblName nvarchar(200), ----要顯示的表或多個表的連接 @fldName nvarchar(500) = '*', ----要顯示的字段列表 @pageSize int = 10, ----每頁顯示的記錄個數 @page int = 1, ----要顯示那一頁的記錄 @pageCount int = 1 output, ----查詢結果分頁后的總頁數 @Counts int = 1 output, ----查詢到的記錄數 @fldSort nvarchar(200) = null, ----排序字段列表或條件 @Sort bit = 0, ----排序方法,0為升序,1為降序(如果是多字段排列Sort指代最后一個排序字段的排列順序(最后一個排序字段不加排序標記)--程序傳參如:' SortA Asc,SortB Desc,SortC ') @strCondition nvarchar(1000) = null, ----查詢條件,不需where @ID nvarchar(150), ----主表的主鍵 @Dist bit = 0 ----是否添加查詢字段的 DISTINCT 默認0不添加/1添加 ) AS SET NOCOUNT ON Declare @sqlTmp nvarchar(1000) ----存放動態生成的SQL語句 Declare @strTmp nvarchar(1000) ----存放取得查詢結果總數的查詢語句 Declare @strID nvarchar(1000) ----存放取得查詢開頭或結尾ID的查詢語句 Declare @strSortType nvarchar(10) ----數據排序規則A Declare @strFSortType nvarchar(10) ----數據排序規則B Declare @SqlSelect nvarchar(50) ----對含有DISTINCT的查詢進行SQL構造 Declare @SqlCounts nvarchar(50) ----對含有DISTINCT的總數查詢進行SQL構造 if @Dist = 0 begin set @SqlSelect = 'select ' set @SqlCounts = 'Count(*)' end else begin set @SqlSelect = 'select distinct ' set @SqlCounts = 'Count(DISTINCT '+@ID+')' end if @Sort=0 begin set @strFSortType=' ASC ' set @strSortType=' DESC ' end else begin set @strFSortType=' DESC ' set @strSortType=' ASC ' end --------生成查詢語句-------- --此處@strTmp為取得查詢結果數量的語句 if @strCondition is null or @strCondition='' --沒有設置顯示條件 begin set @sqlTmp = @fldName + ' From ' + @tblName set @strTmp = @SqlSelect+' @Counts='+@SqlCounts+' FROM '+@tblName set @strID = ' From ' + @tblName end else begin set @sqlTmp = + @fldName + 'From ' + @tblName + ' where (1>0) ' + @strCondition set @strTmp = @SqlSelect+' @Counts='+@SqlCounts+' FROM '+@tblName + ' where (1>0) ' + @strCondition set @strID = ' From ' + @tblName + ' where (1>0) ' + @strCondition end ----取得查詢結果總數量----- exec sp_executesql @strTmp,N'@Counts int out ',@Counts out declare @tmpCounts int if @Counts = 0 set @tmpCounts = 1 else set @tmpCounts = @Counts --取得分頁總數 set @pageCount=(@tmpCounts+@pageSize-1)/@pageSize /**//**當前頁大于總頁數 取最后一頁**/ if @page>@pageCount set @page=@pageCount --/*-----數據分頁2分處理-------*/ declare @pageIndex int --總數/頁大小 declare @lastcount int --總數%頁大小 set @pageIndex = @tmpCounts/@pageSize set @lastcount = @tmpCounts%@pageSize if @lastcount > 0 set @pageIndex = @pageIndex + 1 else set @lastcount = @pagesize --//***顯示分頁 if @strCondition is null or @strCondition='' --沒有設置顯示條件 begin if @pageIndex<2 or @page<=@pageIndex / 2 + @pageIndex % 2 --前半部分數據處理 begin if @page=1 set @strTmp=@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName +' order by '+ @fldSort +' '+ @strFSortType else begin set @strTmp=@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName +' where '+@ID+' <(select min('+ @ID +') from ('+ @SqlSelect+' top '+ CAST(@pageSize*(@page-1) as Varchar(20)) +' '+ @ID +' from '+@tblName +' order by '+ @fldSort +' '+ @strFSortType+') AS TBMinID)' +' order by '+ @fldSort +' '+ @strFSortType end end else begin set @page = @pageIndex-@page+1 --后半部分數據處理 if @page <= 1 --最后一頁數據顯示 set @strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+ CAST(@lastcount as VARCHAR(4))+' '+ @fldName+' from '+@tblName +' order by '+ @fldSort +' '+ @strSortType+') AS TempTB'+' order by '+ @fldSort +' '+ @strFSortType else set @strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName +' where '+@ID+' >(select max('+ @ID +') from('+ @SqlSelect+' top '+ CAST(@pageSize*(@page-2)+@lastcount as Varchar(20)) +' '+ @ID +' from '+@tblName +' order by '+ @fldSort +' '+ @strSortType+') AS TBMaxID)' +' order by '+ @fldSort +' '+ @strSortType+') AS TempTB'+' order by '+ @fldSort +' '+ @strFSortType end end else --有查詢條件 begin if @pageIndex<2 or @page<=@pageIndex / 2 + @pageIndex % 2 --前半部分數據處理 begin if @page=1 set @strTmp=@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName +' where 1=1 ' + @strCondition + ' order by '+ @fldSort +' '+ @strFSortType else begin set @strTmp=@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName +' where '+@ID+' <(select min('+ @ID +') from ('+ @SqlSelect+' top '+ CAST(@pageSize*(@page-1) as Varchar(20)) +' '+ @ID +' from '+@tblName +' where (1=1) ' + @strCondition +' order by '+ @fldSort +' '+ @strFSortType+') AS TBMinID)' +' '+ @strCondition +' order by '+ @fldSort +' '+ @strFSortType end end else begin set @page = @pageIndex-@page+1 --后半部分數據處理 if @page <= 1 --最后一頁數據顯示 set @strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+ CAST(@lastcount as VARCHAR(4))+' '+ @fldName+' from '+@tblName +' where (1=1) '+ @strCondition +' order by '+ @fldSort +' '+ @strSortType+') AS TempTB'+' order by '+ @fldSort +' '+ @strFSortType else set @strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName +' where '+@ID+' >(select max('+ @ID +') from('+ @SqlSelect+' top '+ CAST(@pageSize*(@page-2)+@lastcount as Varchar(20)) +' '+ @ID +' from '+@tblName +' where (1=1) '+ @strCondition +' order by '+ @fldSort +' '+ @strSortType+') AS TBMaxID)' +' '+ @strCondition+' order by '+ @fldSort +' '+ @strSortType+') AS TempTB'+' order by '+ @fldSort +' '+ @strFSortType end end ------返回查詢結果----- exec sp_executesql @strTmp --print @strTmp SET NOCOUNT OFF GO 怎么在數據庫中測試呢?

    ?

    ?

    [sql] view plaincopy print?
  • declare?@pageCount?int???
  • declare?@Counts?int???
  • exec?[dbo].[proc_ListPageInt]?'sysobjects',?'*?',?20,1,@pageCount?output,@Counts?output,'id',?0,'','id',0??
  • print?@pageCount?--這個可有可無???
  • print?@Counts?--這個可有可無???
  • declare @pageCount int declare @Counts int exec [dbo].[proc_ListPageInt] 'sysobjects', '* ', 20,1,@pageCount output,@Counts output,'id', 0,'','id',0 print @pageCount --這個可有可無 print @Counts --這個可有可無 執行效果如下:

    ?

    分頁存儲過程二:

    ?

    [sql] view plaincopy print?
  • USE?[JianKunKingTestDatabase001]???
  • GO???
  • ??
  • /******?Object:?StoredProcedure?[dbo].[A_P_HelpPageShow]?Script?Date:?01/21/2015?19:19:42?******/???
  • SET?ANSI_NULLS?ON???
  • GO???
  • ??
  • SET?QUOTED_IDENTIFIER?ON???
  • GO???
  • ??
  • ??
  • ALTER?PROCEDURE?[dbo].[A_P_HelpPageShow]???
  • (???
  • @tblName?nvarchar(max),?--?表名???
  • @strGetFields?varchar(1000)?=?'*',?--?需要返回的列???
  • @fldName?varchar(255)='',?--?排序的字段名???
  • @PageSize?int?=?10,?--?頁尺寸???
  • @PageIndex?int?=?1,?--?頁碼???
  • @OrderType?bit?=?0,?--?設置排序類型,?非?0?值則降序???
  • @strWhere?varchar(1500)?=?'',?--?查詢條件?(注意:?不要加?where)???
  • @Counts?int?=?0?output?--查詢到的記錄數???
  • )???
  • AS???
  • declare?@strSQL?nvarchar(4000)?--?主語句???
  • declare?@strTmp?nvarchar(110)?--?臨時變量???
  • declare?@strOrder?nvarchar(400)?--?排序類型???
  • declare?@totalRecord?int?--查詢到的記錄數???
  • declare?@SqlCounts?nvarchar(max)?----對總數查詢進行SQL構造???
  • --計算總記錄數???
  • begin???
  • if?@strWhere?!=''???
  • set?@SqlCounts?=?'select?@totalRecord=count(*)?from?'?+?@tblName?+?'?where?'+@strWhere??
  • else???
  • set?@SqlCounts?=?'select?@totalRecord=count(*)?from?'?+?@tblName?+?''???
  • end???
  • ??
  • --print?@strWhere???
  • --print?@SqlCounts???
  • exec?sp_executesql?@SqlCounts,N'@totalRecord?int?OUTPUT',@totalRecord?OUTPUT--計算總記錄數???
  • set?@Counts=@totalRecord???
  • ??
  • begin???
  • ??
  • if?@OrderType?!=?0???
  • begin???
  • set?@strTmp?=?'<(select?min'???
  • set?@strOrder?=?'?order?by?'?+?@fldName?+'?desc'???
  • --如果@OrderType不是0,就執行降序,這句很重要!???
  • end???
  • else???
  • begin???
  • set?@strTmp?=?'>(select?max'???
  • set?@strOrder?=?'?order?by?'?+?@fldName?+'?asc'???
  • end???
  • --print?@strOrder???
  • if?@PageIndex?=?1???
  • begin???
  • if?@strWhere?!=?''???
  • set?@strSQL?=?'select?top?'?+?str(@PageSize)?+'?'+@strGetFields+?'?from?'?+?@tblName?+?'?where?'?+?@strWhere?+?'?'?+?@strOrder???
  • else???
  • set?@strSQL?=?'select?top?'?+?str(@PageSize)?+'?'+@strGetFields+?'?from?'+?@tblName?+?'?'+?@strOrder???
  • --如果是第一頁就執行以上代碼,這樣會加快執行速度???
  • end???
  • else???
  • begin???
  • --以下代碼賦予了@strSQL以真正執行的SQL代碼???
  • set?@strSQL?=?'select?top?'?+?str(@PageSize)?+'?'+@strGetFields+?'?from?'???
  • +?@tblName?+?'?where?'?+?@fldName?+?'?'?+?@strTmp?+?'('+?@fldName?+?')?from?(select?top?'?+?str((@PageIndex-1)*@PageSize)?+?'?'+?@fldName?+?'?from?'?+?@tblName?+?'?'?+?@strOrder?+?')?as?tblTmp)'+?@strOrder???
  • --print?@strSQL???
  • if?@strWhere?!=?''???
  • set?@strSQL?=?'select?top?'?+?str(@PageSize)?+'?'+@strGetFields+?'?from?'???
  • +?@tblName?+?'?where?'?+?@fldName?+?'?'?+?@strTmp?+?'('???
  • +?@fldName?+?')?from?(select?top?'?+?str((@PageIndex-1)*@PageSize)?+?'?'???
  • +?@fldName?+?'?from?'?+?@tblName?+?'?where?'?+?@strWhere?+?'?'???
  • +?@strOrder?+?')?as?tblTmp)?and?'?+?@strWhere?+?'?'?+?@strOrder???
  • end???
  • end???
  • --print?@strSQL???
  • exec?sp_executesql?@strSQL???
  • GO???
  • USE [JianKunKingTestDatabase001] GO /****** Object: StoredProcedure [dbo].[A_P_HelpPageShow] Script Date: 01/21/2015 19:19:42 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ALTER PROCEDURE [dbo].[A_P_HelpPageShow] ( @tblName nvarchar(max), -- 表名 @strGetFields varchar(1000) = '*', -- 需要返回的列 @fldName varchar(255)='', -- 排序的字段名 @PageSize int = 10, -- 頁尺寸 @PageIndex int = 1, -- 頁碼 @OrderType bit = 0, -- 設置排序類型, 非 0 值則降序 @strWhere varchar(1500) = '', -- 查詢條件 (注意: 不要加 where) @Counts int = 0 output --查詢到的記錄數 ) AS declare @strSQL nvarchar(4000) -- 主語句 declare @strTmp nvarchar(110) -- 臨時變量 declare @strOrder nvarchar(400) -- 排序類型 declare @totalRecord int --查詢到的記錄數 declare @SqlCounts nvarchar(max) ----對總數查詢進行SQL構造 --計算總記錄數 begin if @strWhere !='' set @SqlCounts = 'select @totalRecord=count(*) from ' + @tblName + ' where '+@strWhere else set @SqlCounts = 'select @totalRecord=count(*) from ' + @tblName + '' end --print @strWhere --print @SqlCounts exec sp_executesql @SqlCounts,N'@totalRecord int OUTPUT',@totalRecord OUTPUT--計算總記錄數 set @Counts=@totalRecord begin if @OrderType != 0 begin set @strTmp = '<(select min' set @strOrder = ' order by ' + @fldName +' desc' --如果@OrderType不是0,就執行降序,這句很重要! end else begin set @strTmp = '>(select max' set @strOrder = ' order by ' + @fldName +' asc' end --print @strOrder if @PageIndex = 1 begin if @strWhere != '' set @strSQL = 'select top ' + str(@PageSize) +' '+@strGetFields+ ' from ' + @tblName + ' where ' + @strWhere + ' ' + @strOrder else set @strSQL = 'select top ' + str(@PageSize) +' '+@strGetFields+ ' from '+ @tblName + ' '+ @strOrder --如果是第一頁就執行以上代碼,這樣會加快執行速度 end else begin --以下代碼賦予了@strSQL以真正執行的SQL代碼 set @strSQL = 'select top ' + str(@PageSize) +' '+@strGetFields+ ' from ' + @tblName + ' where ' + @fldName + ' ' + @strTmp + '('+ @fldName + ') from (select top ' + str((@PageIndex-1)*@PageSize) + ' '+ @fldName + ' from ' + @tblName + ' ' + @strOrder + ') as tblTmp)'+ @strOrder --print @strSQL if @strWhere != '' set @strSQL = 'select top ' + str(@PageSize) +' '+@strGetFields+ ' from ' + @tblName + ' where ' + @fldName + ' ' + @strTmp + '(' + @fldName + ') from (select top ' + str((@PageIndex-1)*@PageSize) + ' ' + @fldName + ' from ' + @tblName + ' where ' + @strWhere + ' ' + @strOrder + ') as tblTmp) and ' + @strWhere + ' ' + @strOrder end end --print @strSQL exec sp_executesql @strSQL GO 怎么在數據庫中測試呢?

    ?

    ?

    [sql] view plaincopy print?
  • select?count(*)?from?sysobjects?;??
  • declare?@CountsAA?int???
  • exec?[dbo].[A_P_HelpPageShow]?'sysobjects',?'*?',?'id?',20,?1,1,?'?',@CountsAA?output???
  • print?@CountsAA?--這個可有可無??
  • select count(*) from sysobjects ; declare @CountsAA int exec [dbo].[A_P_HelpPageShow] 'sysobjects', '* ', 'id ',20, 1,1, ' ',@CountsAA output print @CountsAA --這個可有可無 執行結果如下:

    ?

    存儲過程二(優化版)

    ?

    [sql] view plaincopy print?
  • USE?[JianKunKingTestDatabase001]??
  • GO??
  • ??
  • /******?Object:??StoredProcedure?[dbo].[A_P_HelpPageShow]????Script?Date:?01/30/2015?20:21:13?******/??
  • SET?ANSI_NULLS?ON??
  • GO??
  • ??
  • SET?QUOTED_IDENTIFIER?ON??
  • GO??
  • ??
  • ??
  • CREATE??PROCEDURE?[dbo].[A_P_HelpPageShow]??
  • (??
  • @TableName???nvarchar(max),???????--?表名??
  • @strGetFields?varchar(1000)?=?'*',?--?需要返回的列???
  • @OrderField?varchar(255)='',??????--?排序的字段名??
  • @PageSize???int?=?10,??????????--?頁尺寸??
  • @PageIndex?int?=?1,??????????--?頁碼??
  • @strWhere?varchar(1500)?=?'',?--?查詢條件?(注意:?不要加?where)??
  • @Counts????int?=?0??output??--查詢到的記錄數??
  • )??
  • AS??
  • declare?@strSQL???nvarchar(4000)???????--?主語句??
  • declare?@totalRecord?int??--查詢到的記錄數??
  • declare?@SqlCounts?nvarchar(max)?????----對總數查詢進行SQL構造??
  • --計算總記錄數??
  • begin??
  • ????if?@strWhere?!=''??
  • set?@SqlCounts?=?'select?@totalRecord=count(*)??from?'?+?@TableName?+?'?where?'+@strWhere??
  • ????else??
  • set?@SqlCounts?=?'select?@totalRecord=count(*)??from?'?+?@TableName?+?''???
  • end???
  • ??
  • exec?sp_executesql?@SqlCounts,N'@totalRecord?int?OUTPUT',@totalRecord?OUTPUT--計算總記錄數??
  • set??@Counts=@totalRecord???
  • ??
  • BEGIN??
  • IF?(@strWhere=''?or?@strWhere?IS?NULL)??
  • SET?@strSQL?=?'Select?*?FROM?(select?'?+?@strGetFields?+?',ROW_NUMBER()?Over(order?by?'?+?@OrderField?+?')?as?rowId?from?'?+?@TableName??
  • ELSE??
  • SET?@strSQL?=?'Select?*?FROM?(select?'?+?@strGetFields?+?',ROW_NUMBER()?Over(order?by?'?+?@OrderField?+?')?as?rowId?from?'?+?@TableName?+?'?where?'?+?@strWhere??????
  • END??
  • ?--處理頁數超出范圍情況???
  • ????IF?@PageIndex<=0???
  • ????????SET?@PageIndex?=?1??
  • ????????
  • ?????--處理開始點和結束點??
  • ????DECLARE?@StartRecord?INT???
  • ????DECLARE?@EndRecord?int??
  • ??????
  • ????SET?@StartRecord?=?(@pageIndex-1)*@PageSize?+?1??
  • ????SET?@EndRecord?=?@StartRecord?+?@PageSize?-?1??
  • ??
  • ?????--繼續合成sql語句??
  • ????SET?@strSQL?=?@strSQL?+?')?as?tempTable?where?rowId?>='?+?CONVERT(VARCHAR(50),@StartRecord)?+?'?and?rowid<=?'?+?CONVERT(VARCHAR(50),@EndRecord)??
  • ??
  • exec?sp_executesql?@strSQL??
  • ??
  • ??
  • GO??
  • USE [JianKunKingTestDatabase001] GO/****** Object: StoredProcedure [dbo].[A_P_HelpPageShow] Script Date: 01/30/2015 20:21:13 ******/ SET ANSI_NULLS ON GOSET QUOTED_IDENTIFIER ON GOCREATE PROCEDURE [dbo].[A_P_HelpPageShow] ( @TableName nvarchar(max), -- 表名 @strGetFields varchar(1000) = '*', -- 需要返回的列 @OrderField varchar(255)='', -- 排序的字段名 @PageSize int = 10, -- 頁尺寸 @PageIndex int = 1, -- 頁碼 @strWhere varchar(1500) = '', -- 查詢條件 (注意: 不要加 where) @Counts int = 0 output --查詢到的記錄數 ) AS declare @strSQL nvarchar(4000) -- 主語句 declare @totalRecord int --查詢到的記錄數 declare @SqlCounts nvarchar(max) ----對總數查詢進行SQL構造 --計算總記錄數 beginif @strWhere !='' set @SqlCounts = 'select @totalRecord=count(*) from ' + @TableName + ' where '+@strWhereelse set @SqlCounts = 'select @totalRecord=count(*) from ' + @TableName + '' end exec sp_executesql @SqlCounts,N'@totalRecord int OUTPUT',@totalRecord OUTPUT--計算總記錄數 set @Counts=@totalRecord BEGIN IF (@strWhere='' or @strWhere IS NULL) SET @strSQL = 'Select * FROM (select ' + @strGetFields + ',ROW_NUMBER() Over(order by ' + @OrderField + ') as rowId from ' + @TableName ELSE SET @strSQL = 'Select * FROM (select ' + @strGetFields + ',ROW_NUMBER() Over(order by ' + @OrderField + ') as rowId from ' + @TableName + ' where ' + @strWhere END--處理頁數超出范圍情況 IF @PageIndex<=0 SET @PageIndex = 1--處理開始點和結束點DECLARE @StartRecord INT DECLARE @EndRecord intSET @StartRecord = (@pageIndex-1)*@PageSize + 1SET @EndRecord = @StartRecord + @PageSize - 1--繼續合成sql語句SET @strSQL = @strSQL + ') as tempTable where rowId >=' + CONVERT(VARCHAR(50),@StartRecord) + ' and rowid<= ' + CONVERT(VARCHAR(50),@EndRecord)exec sp_executesql @strSQLGO 拓展:http://blog.csdn.net/jiankunking/article/details/43339301

    轉載于:https://www.cnblogs.com/freeliver54/p/7323672.html

    總結

    以上是生活随笔為你收集整理的[转]Sql Server 分页存储过程的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。