alter PROCEDURE Pages
@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
@ResultCount varchar(24)-------显示的结果总数
AS
BEGIN
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
if(len(rtrim(ltrim(@ResultCount)))>0)
set @ResultCount='set rowcount '+ltrim(rtrim(@ResultCount))
set @topRow= rtrim(ltrim(str(@PageSize*(@CurrentPage-1))))
set @tempPageSize= rtrim(ltrim(str(@PageSize)))
exec('SET NOCOUNT ON '+'
'+@ResultCount+'
set rowcount '+@tempPageSize+'
select * from (select row_number() over ('+@Order+') rownumber,'+@Fields+' from '+@TableNames+' where '+@Filter+') tempTable where rownumber>'+@topRow+'
set rowcount 0')
end
@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
@ResultCount varchar(24)-------显示的结果总数
AS
BEGIN
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
if(len(rtrim(ltrim(@ResultCount)))>0)
set @ResultCount='set rowcount '+ltrim(rtrim(@ResultCount))
set @topRow= rtrim(ltrim(str(@PageSize*(@CurrentPage-1))))
set @tempPageSize= rtrim(ltrim(str(@PageSize)))
exec('SET NOCOUNT ON '+'
'+@ResultCount+'
set rowcount '+@tempPageSize+'
select * from (select row_number() over ('+@Order+') rownumber,'+@Fields+' from '+@TableNames+' where '+@Filter+') tempTable where rownumber>'+@topRow+'
set rowcount 0')
end