數據庫分頁的幾種方式


1. 通過數據庫的ROW_NUMBER()Over() 函數

eg:

   select * from (select ROW_NUMBER()Over(order by dbkey desc) as rowid,UsersLoginName from PayClerk) B WHERE rowid between 1 and 20

    pagesize*(pagindex-1)   ----- pagesize*(pageindex)

2. 通過offset  x fetch next  (SQLServer2012 或者更高版本)

         select * from TAccount order by DbKey desc offset 0 rows fetch next 20 rows only

3.通過  top  not in

      select top 20 * from TAccount  where dbkey not in (select top 40 dbkey from TAccount  order by dbkey desc) order by dbkey desc

4.通過存儲過程

Create PROCEDURE ProAccount
  @pagesize int,   //頁大小
  @pageindex int   //頁索引
AS
  BEGIN
    declare @currentRowNumberIndex int    //當前分頁起始Row_Number
    set @currentRowNumberIndex =@pagesize*(@pageindex-1)+1
    select * from (select ROW_NUMBER()over(order by dbkey desc) as rowid,* from TAccount) B where rowid between @currentRowNumberIndex and @pageindex*@pagesize
  END
GO

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM