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=''
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('
declare @temptable table(rownum int identity(1,1),Gid varchar(36))'+'
declare @datatable table(Gid varchar(36))'+'
declare @date datetime'+'
set @date=getdate()'+'
SET NOCOUNT ON '+'
'+@ResultCount+'
insert into @temptable(Gid) select '+@PrimaryKey+' from '+@TableNames+' where '+@Filter+@Order+'
set rowcount '+@tempPageSize+'
insert into @datatable(Gid) select Gid from @temptable where rownum>'+@topRow+'
select '+@Fields+' from '+@TableNames+' where '+@Filter+' and '+@PrimaryKey+' in (select Gid from @datatable)'+@Order+'
set rowcount 0'+'
print(datediff(ms,@date,getdate()))')
--declare @datatable table(Gid varchar(12))'+'
---insert into @datatable(Gid) select Gid from @temptable where rownum>'+@topRow+'
--set rowcount '+@tempPageSize+'
--+'set rowcount 0'
--print('insert into @temptable(Gid) select '+@PrimaryKey+' from '+@TableNames+' where '+@Filter+@Order)
---print('select '+@Fields+' from '+@TableNames+' where '+@Filter+' and '+@PrimaryKey+' in(select Gid from @datatable) '+@Order)
end
GO