用首尾ID法寫分頁預存程序

來源:互聯網
上載者:User
CREATE PROC GetList
(
      @iCurPage INT,
      @iPageSize INT
)
AS
   --定義局部變數
   DECLARE @iBeginID         INT,
         @iEndID           INT,
          @iRootRecordCount INT,
          @iPageCount       INT,
          @iRowCount        INT
   
   SET NOCOUNT ON
   
   --求總共根貼數
   SELECT @iRootRecordCount = COUNT(*) FROM Products
       
   --判斷頁數是否正確
   --IF (@iCurPage - 1) * @iPageSize > @iRootRecordCount
   --RETURN -1

   --求開始rootID
   SET @iRowCount = (@iCurPage - 1) * @iPageSize + 1
   SET ROWCOUNT @iRowCount
   SELECT  @iBeginID = ProductID FROM Products ORDER BY ProductID DESC

   --結束rootID
   SET @iRowCount = @iCurPage * @iPageSize
     SET ROWCOUNT @iRowCount
   SELECT @iEndID = ProductID FROM Products ORDER BY ProductID DESC
   
   --恢複系統變數
   SET ROWCOUNT 0
   SET NOCOUNT OFF
   --SELECT @iBeginID AS beginID,@iEndID AS endID

   SELECT *
   FROM Products
   WHERE ProductID BETWEEN @iEndID AND @iBeginID ORDER BY ProductID DESC

   RETURN(@@rowcount)
GO


--調用
    USE Northwind
    DECLARE @iCurPage INT,@iPageSize INT
    SET @iCurPage = 1
    SET @iPageSize = 10
    exec GetList 1,10

聯繫我們

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