sqlserver的四種分頁方式

來源:互聯網
上載者:User

標籤:roc   儲存   arc   sql2012   not   proc   har   exec   src   

 

第一種:ROW_NUMBER() OVER()方式

select * from (
    select *, ROW_NUMBER() OVER(Order by ArtistId ) AS RowId from ArtistModels
  ) as b

      where RowId between 10 and 20
  ---where RowId BETWEEN 當前頁數-1*條數 and 頁數*條數---     

執行結果是:

第二種方式:offset fetch next方式(SQL2012以上的版本才支援:推薦使用 )

select * from ArtistModels  order by ArtistId offset 4 rows fetch next 5 rows only
                  --order by ArtistId offset 頁數 rows fetch next 條數 rows only ----

執行結果是:

第三種方式:--top not in方式 (適應於資料庫2012以下的版本)

select top 3 * from ArtistModels
where ArtistId not in (select top 15 ArtistId from ArtistModels)

------where Id not in (select top 條數*頁數  ArtistId  from ArtistModels)  

執行結果:

第四種方式:用預存程序的方式進行分頁  

CREATE procedure page_Demo
@tablename varchar(20),
@pageSize int,
@page int
AS
declare @newspage int,
@res varchar(100)
begin
set @[email protected]*(@page - 1)
set @res=‘select * from ‘ [email protected]+ ‘ order by ArtistId offset ‘+CAST(@newspage as varchar(10)) +‘ rows fetch next ‘+ CAST(@pageSize as varchar(10)) +‘ rows only‘
exec(@res)
end
EXEC page_Demo @tablename=‘ArtistModels‘,@pageSize=3,@page=5

執行結果:

  ps:今天搞了一下午的分頁,通過上網查資料和自己的實驗,總結了四種分頁方式供大家參考,有問題大家一起交流學習

sqlserver的四種分頁方式

聯繫我們

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