實用單表千萬級分頁儲存過程一(不敢獨享,特此分享)

iSQlServer發表於2009-10-14

實用單表千萬級分頁儲存過程一(不敢獨享,特此分享)

此儲存過程只適合於主鍵查詢

意思就是其中變數@strSortKey和@strSortField必須都是主鍵,如:ID,ID desc


本儲存過程本人做了小修改


/**********************************************************************************************
********************通過指定的條件分頁查詢資料表【TableName or ViewName】記錄******************
**********************************************************************************************/
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id (N'[dbo].[ThePaginationProcedureIsUsedInPrimaryKey]') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE [dbo].[ThePaginationProcedureIsUsedInPrimaryKey]
GO
--引數說明
-------------------------------------------------------------
/*
@strTable --要顯示的表或多個表的連線
@strField --要查詢出的欄位列表,*表示全部欄位
@intTop --最多讀取記錄數
@pageSize --每頁顯示的記錄個數
@pageIndex --要顯示那一頁的記錄
@strWhere --查詢條件,不需where
@strSortKey --用於排序的主鍵
@strSortField --用於排序,如:id desc (多個id desc,dt asc)
@strOrderBy --排序,0-順序,1-倒序
@pageCount --查詢結果分頁後的總頁數
@RecordCount --查詢到的總記錄數
@UsedTime --耗時測試時間差
*/
CREATE PROCEDURE [dbo].[ThePaginationProcedureIsUsedInPrimaryKey]
@strTable varchar(max) = '[dbo].[TableOrView]',
@strField varchar(max) = '*',
@intTop int = max,
@pageSize int = 45,
@pageIndex int = 1,
@strWhere varchar(max) = '1=1',
@strSortKey varchar(max) = 'TbVe_id',
@strSortField varchar(max) = 'TbVe_date DESC',
@strOrderBy bit = 1,
@pageCount int OUTPUT,
@RecordCount int OUTPUT,
@UsedTime int OUTPUT
AS
SET NOCOUNT ON
Declare @sqlcount INT
Declare @timediff DATETIME
select @timediff=getdate()
Begin Tran
DECLARE @sql nvarchar(max),@where1 varchar(max),@where2 varchar(max)
IF @strWhere is null or rtrim(@strWhere)=''
BEGIN--沒有查詢條件
SET @where1=' WHERE '
SET @where2=' '
END
ELSE
BEGIN--有查詢條件
SET @where1=' WHERE ('+@strWhere+') AND ' --本來有條件再加上此條件
SET @where2=' WHERE ('+@strWhere+') ' --原本沒有條件而加上此條件
END
--SET @sql='SELECT @intResult=COUNT(*) FROM '+@strTable+@where2
IF @intTop<=0
BEGIN
SET @sql='SELECT @sqlcount=COUNT(*) FROM (select '+@strSortKey+' from '+ @strTable + @where2 +') As tmptab'
END
ELSE
BEGIN
SET @sql='SELECT @sqlcount=COUNT(*) FROM (select top '+ cast(@intTop as varchar(max)) +' '+@strSortKey+' from '+ @strTable + @where2 +') As tmptab'
END
--print @sql

EXEC sp_executesql @sql,N'@sqlcount int OUTPUT',@sqlcount OUTPUT --計算總記錄數
SELECT @pageCount=CEILING((@sqlcount+0.0)/@pageSize) --計算總頁數
SELECT @RecordCount = @sqlcount --設定總記錄數
IF @pageIndex=1 --第一頁
BEGIN
SET @sql='SELECT TOP '+CAST(@pageSize AS varchar(max))+' '+@strField+' FROM '+@strTable+
@where2+'ORDER BY '+ @strSortField
END
Else
BEGIN
IF @strOrderBy=0
SET @sql='SELECT TOP '+CAST(@pageSize AS varchar(max))+' '+@strField+ ' FROM '+@strTable+@where1+@strSortKey+'>(SELECT MAX('+@strSortKey+') '+ ' FROM (SELECT TOP '+CAST(@pageSize*(@pageIndex-1) AS varchar(max))+' '+
@strSortKey+' FROM '+@strTable+@where2+'ORDER BY '+@strSortField+') t) ORDER BY '+@strSortField
ELSE
SET @sql='SELECT TOP '+CAST(@pageSize AS varchar(max))+' '+@strField+' FROM '+@strTable+@where1+@strSortKey+'@strSortKey+' FROM '+@strTable+@where2+'ORDER BY '+@strSortField+') t) ORDER BY '+@strSortField+''
END
EXEC(@sql)
--print @sql
If @@Error <> 0
Begin
RollBack Tran
Return -1
End
Else
Begin
Commit TRAN
set @UsedTime = datediff(ms,@timediff,getdate())
--select datediff(ms,@timediff,getdate()) as 耗時
Return @sqlcount
End
GO

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/16436858/viewspace-616531/,如需轉載,請註明出處,否則將追究法律責任。

相關文章