SqlServer2005通用分页存储过程
从网上看了很多分页的存储过程,没有发现一个所有表通用的!也许有更高的招,只是我自己不知道而已,特发此贴,以集思广益。
我先发一个我自己写的通用分页存储过程,希望前辈、哥哥姐姐、弟弟妹妹们批评指正,本人不胜感激。
代码
--@Columns 要得到的列
--@TableName 表名(可做联合查询)
--@Condition 查询条件
--@OrderBy 排序规则
--@PageNum 第几页
--@PageSize 每页有多少务记录
--@PageCount 输出总页数
--@RecordCount 输出总记录数
Create Procedure proc_CurrencyPage
(
@Columns varchar(max),
@TableName varchar(max),
@Condition varchar(max),
@OrderBy varchar(max),
@PageNum int,
@PageSize int,
@PageCount int output,
@RecordCount bigint output
)
AS
DECLARE @Sql nvarchar(max);
Set @Sql = 'Select @CountOut = Count(*) From ' + @TableName + ' Where ' + @Condition;
EXEC sp_executesql @Sql,N'@CountOut INT OUTPUT',@CountOut = @RecordCount OUTPUT;
Set @PageCount = @RecordCount / @PageSize;
IF(@RecordCount % @PageSize > 0) Set @PageCount = @PageCount + 1;
IF(@PageNum < 1) Set @PageNum = 1;
IF(@PageNum > @PageCount) Set @PageNum = @PageCount;
Declare @BRowNum int,@ERowNum int;
Set @BRowNum = (@PageNum - 1) * @PageSize;
Set @ERowNum = @BRowNum + @PageSize;
Set @Sql =
'Select * From (
Select '+@Columns+', ROW_NUMBER() Over(Order By ' + @OrderBy + ') As RowNum
From ' + @TableName + '
Where ' + @Condition + '
) as TempT
Where RowNum > ' + Convert(varchar(10),@BRowNum) + '
And
RowNum <= ' + Convert(varchar(10),@ERowNum) + '
Order By ' + @OrderBy;
Exec(@Sql);
GO
--测试
Declare @Columns varchar(max)
Declare @TableName varchar(max)
Declare @Condition varchar(max)
Declare @OrderBy varchar(max)
Declare @PageNum int
Declare @PageSize int
Declare @PageCount int
Declare @RecordCount bigint
set @Columns = 'ID,ReportID,ReportCondition'
set @TableName = '[PSYT_TS_ReportSort]'
set @Condition = 'ReportSort = ''Z'''
set @ORDERBY = 'ID'
set @PageNum = 2
set @PageSize=20
Exec proc_CurrencyPage @Columns,@TableName,@Condition,@ORDERBY,@PageNum,@P