代碼
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