sql server2000分頁方式很多,效率嗎 當到達10萬級以上就有些吃力了,這裡暫時不例出來了
sql server2005以上就有了row_number 也是一大進步,詳情如下參考
複製代碼 代碼如下:Sql Server2005通用分頁預存程序
CREATE PROCEDURE [dbo].[Common_GetPagedList]
(
@TableName nvarchar(100), --表名
@ColumnNames nvarchar(1000) = '*', --欄位名集合(全部欄位為*,其它逗號分隔)
@OrderClause nvarchar(1000), --排序從句(不包含order by)
@WhereClause nvarchar(1000) =N' 1=1 ', --條件從句(不包含where)
@PageSize int = 0, --每頁記錄數(0為所有)
@PageIndex int = 1, --頁索引(從1開始)
@TotalRecord int output --返回總記錄數
)
AS
BEGIN
if (@ColumnNames is null or @ColumnNames='') set @ColumnNames=' * '
if (@WhereClause is null or @WhereClause='') set @WhereClause=' 1=1 '
if (@OrderClause is null or @OrderClause='') set @OrderClause=' Id desc '
--處理開始點和結束點
Declare @StartRecord int;
Declare @EndRecord int;
Declare @TotalCountSql nvarchar(1200);
Declare @SqlString nvarchar(4000);
--統計記錄
if(@TotalRecord is null OR @TotalRecord>=0)
begin
SET @TotalCountSql= N'select @TotalRecord = count(*) from ' + @TableName +' where '+@WhereClause;
--select @TotalCountSql
EXEC sp_executesql @totalCountSql,N'@TotalRecord int out',@TotalRecord output;--返回總記錄數
end
if @PageSize>0
begin
if @PageIndex<1 set @PageIndex=1
set @StartRecord = (@PageIndex-1)*@PageSize + 1
set @EndRecord = @StartRecord + @PageSize - 1
set @SqlString = N'select row_number() over (order by '+ @OrderClause +') as rowId,'+@ColumnNames+' from '+ @TableName+' where '+@WhereClause;
set @SqlString ='select * from (' + @SqlString + ') as t where rowId between ' + ltrim(str(@StartRecord)) + ' and ' + ltrim(str(@EndRecord));
end
else
begin
set @SqlString='select '+@ColumnNames+' from '+ @TableName+' where '+@WhereClause +' order by '+@OrderClause
end
--select @SqlString
Exec(@SqlString)
END