百萬級SQL分頁預存程序

來源:互聯網
上載者:User

/*百萬級SQL分頁預存程序,請尊重原作者資訊.我只是更新一部分資料,原作者在分頁的處理過程中有一點錯誤.檢測了程式.發現並休正過來的.
Descript:分頁預存程序
Author:Blue.Dream
Date:2004-8-18 21:01
Update:XQF222
Date:18:11 2007-6-29
*/
CREATE  PROCEDURE SP_MillionPageData4(
@tblName  nvarchar(200),            ----要顯示的表或多個表的串連
@fldName  nvarchar(200) = '*',     ----要顯示的欄位列表
@pageSize int = 100,                   ----每頁顯示的記錄個數
@page  int = 1,            ----要顯示那一頁的記錄
@pageCount int = 1 output,          ----查詢結果分頁後的總頁數
@Counts int = 1 output,            ----查詢到的記錄數
@ID  nvarchar(50),    ----主表的主鍵
@fldSort nvarchar(100) = null,       ----排序欄位列表或條件
@Sort  bit = 0,             ----排序方法,0為升序,1為降序
@strCondition nvarchar(200) = null ----查詢條件,不需where
)
AS
SET NOCOUNT ON
Declare @sqlTmp nvarchar(1000)  ----存放動態產生的SQL語句
Declare @strTmp nvarchar(1000)  ----存放取得查詢結果總數的查詢語句
Declare @strID  nvarchar(1000)  ----存放取得查詢開頭或結尾ID的查詢語句
Declare @sqlSort nvarchar(200)  ----存放臨時產生的排序條件
Declare @intCounts int   ----要移動的記錄數
Declare @BeginID int   ----開始的ID
Declare @EndID   int   ----結束的ID

 --------首先產生排序方法---------
if @Sort=0  --升序
begin
   if not(@fldSort is null)
  set @sqlSort = ' Order by ' + @fldSort
   else
  set @sqlSort = ' Order by ' + @ID 
end
else   --降序
begin
 if not(@fldSort is null)
  set @sqlSort = ' Order by ' + @fldSort + ' DESC' 
 else
  set @sqlSort = ' Order by ' + @ID + ' DESC '
end

--------產生查詢語句--------
--此處@strTmp為取得查詢結果數量的語句
if @strCondition is null --沒有設定顯示條件
 begin
 set @sqlTmp =  @fldName + ' From ' + @tblName
 set @strTmp = 'select @Counts=Count(' + @ID + ') FROM '+@tblName
 set @strID = ' From ' + @tblName
 end
else
 begin
 set @sqlTmp = + @fldName + 'From ' + @tblName + ' where ' + @strCondition
 set @strTmp = 'select @Counts=Count(' + @ID + ') FROM '+@tblName + ' where ' + @strCondition
 set @strID = ' From ' + @tblName + ' where ' + @strCondition
 end
 
----取得查詢結果總數量-----
exec sp_executesql @strTmp,N'@Counts int out ',@Counts out

--取得分頁總數
if @Counts <= @pageSize
 set @pageCount = 1
else
 set @pageCount = (@Counts / @pageSize) + 1

--計算要移動的記錄數
if @page = 1 --加快處理
begin
-----取得分頁後此頁的第一條記錄的ID
set @strID = 'select @BeginID=' + @ID + ' ' + @strID
set @intCounts = 1
set rowcount  @intCounts
exec sp_executesql @strID,N'@BeginID int out ',@BeginID out
-----取得分頁後此頁的最後一條記錄的ID
set rowcount  @pageSize
exec sp_executesql @strID,N'@BeginID int out ',@EndID out
end

else
begin

-----取得分頁後此頁的第一條記錄的ID
set @strID = 'select @BeginID=' + @ID + ' ' + @strID
set @intCounts =(@page-1)* @pageSize+1
set rowcount  @intCounts
exec sp_executesql @strID,N'@BeginID int out ',@BeginID out

-----取得分頁後此頁的最後一條記錄的ID
set @intCounts = @intCounts + @pageSize - 1
set rowcount  @intCounts
exec sp_executesql @strID,N'@BeginID int out ',@EndID out
set @intCounts = (@pageCount-1)* @pageSize
end

------恢複系統設定-----
set rowcount  0
SET NOCOUNT ON

------返回查詢結果-----
if @strCondition is null
 set @strTmp = 'select ' + @sqlTmp + ' where ' + @ID + ' between ' + str(@BeginID) + ' and ' + str(@EndID)
else
 set @strTmp = 'select ' + @sqlTmp + ' where ' + @ID +' (between ' + str(@BeginID) + ' and ' + str(@EndID) + ')  and ' + @strCondition

if not(@sqlSort is null)
set @strTmp = @strTmp + @sqlSort
exec sp_executesql @strTmp
GO
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.