Sqlserver分頁預存程序

來源:互聯網
上載者:User

CREATE 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),-------顯示的結果總數
@distinct  varchar(12)         -------是否去重複
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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.