使用ROW_NUMBER進行的快速分頁

來源:互聯網
上載者:User

使用ROW_NUMBER進行的快速分頁

Linq分頁的方法用到Skip(),Take()。

然而,用SQL指令碼進行分頁如何寫呢?

首先我們可以通過ROW_NUMBER() OVER進行排序並得到一個帶序號的視圖,再通過序號確定要尋找的分頁資料

例:

DECLARE @pageSize INT ;
DECLARE @pageIndex INT ;
SET @pageSize = 5
SET @pageIndex = 2 ;
 --第二頁,每頁顯示5條資料           
SELECT  * FROM
(
SELECT ROW_NUMBER() OVER (ORDER BY CreateDate ASC) AS 'rowNumber' ,* FROM dbo.Task_Info
) AS temp
WHERE rowNumber BETWEEN ( ( ( @pageIndex - 1 ) * @pageSize ) + 1 ) AND ( @pageIndex * @pageSize )

本文永久更新連結地址:

相關文章

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.