在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

 



相关文章:

  • 2022-01-07
  • 2021-11-19
  • 2022-01-25
  • 2021-12-04
  • 2022-12-23
  • 2021-09-25
猜你喜欢
  • 2021-12-04
  • 2021-06-06
  • 2021-07-10
  • 2021-09-16
  • 2021-08-24
  • 2021-08-15
相关资源
相似解决方案