Alter PROCEDURE [dbo].[usp_DataPages]
@TableNames VARCHAR(200), --表名,可以是多个表,但不能用别名
@PrimaryKey VARCHAR(100), --主键,可以为空,但@Order为空时该值不能为空
@Fields VARCHAR(800), --要取出的字段,可以是多个表的字段,可以为空,为空表示select *
@PageSize INT, --每页记录数
@CurrentPage INT, --当前页,0表示第1页
@Filter VARCHAR(200) = '', --条件,可以为空,不用填 where
@Order VARCHAR(200) = '', --排序,可以为空,为空默认按主键升序排列,不用填 order by
@RecountCount int=0 output
AS
BEGIN
/*
declare @RecountCount int
exec [usp_DataPages]
@TableNames='DE_Dict',
@PrimaryKey='',
@Fields='*',
@PageSize=10,
@CurrentPage=1,
@Filter='',
@Order='DictOrder desc',
@RecountCount=@RecountCount output
print @RecountCount
*/
set nocount on;
declare @topRow varchar(12)
declare @tempPageSize varchar(12)
if(len(@Order)>0)
begin
set @Order=' order by '+@Order
end
else
begin
set @Order=' order by '+@PrimaryKey
end
if (len(@Filter)<1)
begin
set @Filter=' 1=1'
end
if(@CurrentPage-1<=0)
set @CurrentPage=0
set @topRow= rtrim(ltrim(str(@PageSize*(@CurrentPage-1))))
set @tempPageSize= rtrim(ltrim(str(@PageSize)))
exec('set rowcount '+@tempPageSize+'select * from (select row_number() over ('+@Order+') rownumber,'+@Fields+' from '+@TableNames+' where '+@Filter+') tempTable where rownumber>'+@topRow+'set rowcount 0')
/*计算总页数*/
declare @sql nvarchar(max)
set @sql='select @RecountCount=count(1) from '+@TableNames+' where '+@Filter
set @RecountCount=0
exec sp_executesql @sql,N'@RecountCount int output',@RecountCount=@RecountCount output
set nocount off;
end