[MSSQL]mssql海量高效分頁儲存過程

廈門德仔發表於2012-02-16
 
CREATE PROCEDURE [dbo].[PROC_GetListRecordByPage]
@tblName VARCHAR(255), -- 表名
@fldName VARCHAR(255), -- 主鍵欄位名
@PageSize INT = 10,-- 頁尺寸
@PageIndex INT = 1, -- 頁碼
@IsReCount INT = 0, -- 0 返回記錄總數; 1只返回資料; 2 都返回
@OrderType BIT = 0, -- 設定排序型別, 非 0 值則降序
@strWhere VARCHAR(3000) = '', -- 查詢條件 (注意: 不要加 where)
@returnFld VARCHAR(1000) = '*' --需要返回的列
AS
BEGIN
DECLARE @tempSql VARCHAR(6000) -- 臨時語句
DECLARE @strSqlAll VARCHAR(6000) -- 主語句
DECLARE @strSqlCnt VARCHAR(400) -- 資料數量
DECLARe @OrderTypeStr NVARCHAR(20)

IF @OrderType != 0 or @OrderType is null
SET @OrderTypeStr = 'DESC'
ELSE
SET @OrderTypeStr = 'ASC'


SET @tempSql = 'SELECT ROW_NUMBER() OVER (ORDER BY [' + @fldName + '] ' + @OrderTypeStr + ') AS rowId,' + @returnFld + ' FROM [' + @tblName + '] '

IF @strWhere != ''
SET @tempSql = @tempSql + ' WHERE ' + @strWhere

SET @strSqlAll = 'SELECT * FROM (' + @tempSql + ') t WHERE rowId BETWEEN ' + CAST(((@PageIndex - 1) * @PageSize + 1) AS VARCHAR)
+ ' AND ' + CAST((@PageIndex * @PageSize ) AS VARCHAR);
--PRINT @strSqlAll
EXEC (@strSqlAll)
IF @IsReCount != 1
IF @strWhere != ''
SET @strSqlCnt = 'SELECT COUNT(1) AS Total FROM [' + @tblName + '] WHERE ' + @strWhere
ELSE
SET @strSqlCnt = 'SELECT COUNT(1) AS Total FROM [' + @tblName + ']'
EXEC (@strSqlCnt)

END 

 
 
或者
 
CREATE PROCEDURE [dbo].[PROC_GetListByPage_Sql2000]
@tblName varchar(255), -- 表名
@fldName varchar(255), -- 排序欄位名 隨機為newid()
@PageSize int = 10, -- 頁尺寸
@PageIndex int = 1, -- 頁碼
@IsReCount int = 0, -- 0 返回記錄總數; 1只返回資料; 2 都返回
@OrderType bit = 0, -- 設定排序型別, 非 0 值則降序
@strWhere varchar(3000) = '', -- 查詢條件 (注意: 不要加 where)
@returnFld varchar(1000) = '*' --需要返回的列
AS

declare @strSQL varchar(6000) -- 主語句
declare @strSQL1 varchar(6000) -- 主語句
declare @strTmp varchar(2000) -- 臨時變數
declare @strOrder varchar(400) -- 排序型別

if @OrderType != 0
begin
set @strTmp = '<(select min'
set @strOrder = ' order by ' + @fldName +' desc'
end
else
begin
set @strTmp = '>(select max'
set @strOrder = ' order by ' + @fldName +' asc'
end

set @strSQL = 'select top ' + str(@PageSize) + @returnFld + ' from ['
+ @tblName + '] where [' + @fldName + ']' + @strTmp + '(['
+ @fldName + ']) from (select top ' + str((@PageIndex-1)*@PageSize) + ' ['
+ @fldName + '] from [' + @tblName + ']' + @strOrder + ') as tblTmp)'
+ @strOrder

if @strWhere != ''
set @strSQL = 'select top ' + str(@PageSize) + @returnFld + ' from ['
+ @tblName + '] where [' + @fldName + ']' + @strTmp + '(['
+ @fldName + ']) from (select top ' + str((@PageIndex-1)*@PageSize) + ' ['
+ @fldName + '] from [' + @tblName + '] where ' + @strWhere + ' '
+ @strOrder + ') as tblTmp) and ' + @strWhere + ' ' + @strOrder

if @PageIndex = 1
begin
set @strTmp =''
if @strWhere != ''
set @strTmp = ' where ' + @strWhere

set @strSQL = 'select top ' + str(@PageSize) + @returnFld + ' from ['
+ @tblName + ']' + @strTmp + ' ' + @strOrder
end
--print @strSQL
exec (@strSQL)

if @IsReCount != 1
if @strWhere != ''
set @strSQL1 = 'select count(*) as Total from [' + @tblName + ']'+' where ' + @strWhere
else
set @strSQL1 = 'select count(*) as Total from [' + @tblName + ']'

exec (@strSQL1) 


相關文章