一個比較不錯的儲存過程分頁

weixin_34119545發表於2006-11-10

    在ms sql server 中,可以充分利用儲存過程進行分頁的優化,下面是一個不錯的例子,其中充分利用了
set rowcount的功能。儲存過程中,可以向@startrowindex傳入第N頁的頁碼,@maximumrow是每頁的記錄條數

CREATE PROCEDURE [usp_GetProducts]

@startRowIndex int,
@maximumRows int,
@totalRows int OUTPUT

AS

DECLARE @first_id int, @startRow int

SET @startRowIndex =  (@startRowIndex - 1)  * @maximumRows+1

 

SET ROWCOUNT @startRowIndex

SELECT @first_id = ProductID FROM Products ORDER BY ProductID

PRINT @first_id

SET ROWCOUNT @maximumRows

SELECT ProductID, ProductName FROM Products WHERE
ProductID >= @first_id
ORDER BY ProductID
 
SET ROWCOUNT 0

-- GEt the total rows

SELECT @totalRows = COUNT(ProductID) FROM Products
GO

 



相關文章