Sql萬能分頁代碼

來源:互聯網
上載者:User

標籤:查詢條件   int   HERE   erb   name   記錄   sel   tab   lse   

go  ----萬能分頁代碼
create procedure [dbo].[sp_datapager]
@pagesize int,--每一頁的大小
@pageindex int,--頁碼數
@tablename varchar(Max),--表的名稱
@keycolumn varchar(20),---主鍵id
@columns varchar(200),--要查詢出列的名稱
@where varchar(200),---查詢條件
@orderby varchar(100),---排序方式
@recordcount int out--輸出參數,非0則返回要查詢表的總記錄數

as
declare @sql nvarchar(3000)
declare @rcsql nvarchar(1000)
set @rcsql=‘select @rc=count(*) from ‘[email protected]
set @sql=‘select top ‘+convert(varchar(3),@pagesize)+‘ ‘+ @columns+‘ from ‘+
@tablename +‘ where ‘[email protected]+‘ not in(select top ‘+
convert(varchar(10),(@pageindex-1)*@pagesize)+‘ ‘[email protected]+
‘ from ‘[email protected]+‘)‘
if (@where!=‘‘)
begin
set @rcsql=‘select @rc=count(*) from ‘[email protected]+‘ where ‘[email protected]
set @sql=‘select top ‘+convert(varchar(3),@pagesize)+ @columns+‘ from ‘+
@tablename +‘ where ‘[email protected]+‘ not in(select top ‘+
convert(varchar(10),(@pageindex-1)*@pagesize)+‘ ‘[email protected]+
‘ from ‘[email protected]+‘ where ‘[email protected]+‘) and ‘[email protected]
end
if (@orderby!=‘‘)
begin
if (@where!=‘‘)
begin
set @sql=‘select top ‘+convert(varchar(3),@pagesize)+ @columns+‘ from ‘+
@tablename +‘ where ‘[email protected]+‘ not in(select top ‘+
convert(varchar(10),(@pageindex-1)*@pagesize)+‘ ‘[email protected]+
‘ from ‘[email protected]+‘ where ‘[email protected]+‘ order by ‘[email protected]+‘) and ‘+
@where+‘ order by ‘ [email protected]
end
else
begin
set @sql=‘select top ‘+convert(varchar(3),@pagesize)+ @columns+‘ from ‘+
@tablename +‘ where ‘[email protected]+‘ not in(select top ‘+
convert(varchar(10),(@pageindex-1)*@pagesize)+‘ ‘[email protected]+
‘ from ‘[email protected]+‘ order by ‘[email protected]+‘)‘+‘ order by ‘ [email protected]
end

end
declare @param nvarchar(100)
set @param=‘@rc int output‘
exec sp_executesql @sql
exec sp_executesql @rcsql,@param,@[email protected] output

Sql萬能分頁代碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.