随着SQL Server的发布,其中的一些排序函数使得开发人员编写数据分页程序变得更加简单和高效,本文将着重介绍新增加的ROW-NUMBER排序函数,它会根据你指定的分类标准将结果数据集进行分类,同时给数据集分配连续的页面。
CREATE PROCEDURE usp_SalesRecords
(
@PageSize FLOAT,
@TargetPage SMALLINT
)
AS
BEGIN
WITH Sales_CTE(PageNumber, SaleID, Product, SaleDate, SalePrice)
AS
(
SELECT
CEILING((ROW_NUMBER() OVER (ORDER BY SaleDate ASC))/@PageSize) AS PageNumber, SaleID, Product, SaleDate, SalePrice
FROM SalesHistory FROM SalesHistory
)
SELECT
PageNumber, SaleID, Product, SaleDate, SalePrice
FROM
Sales_CTE
WHERE
PageNumber = @Targetpage
ENDCREATE PROCEDURE usp_SalesRecords
(
@PageSize FLOAT,
@TargetPage SMALLINT
)
AS
BEGIN
WITH Sales_CTE(PageNumber, SaleID, Product, SaleDate, SalePrice)
AS
(
SELECT
CEILING((ROW_NUMBER() OVER (ORDER BY SaleDate ASC))/@PageSize) AS PageNumber, SaleID, Product, SaleDate, SalePrice
FROM SalesHistory FROM SalesHistory
)
SELECT
PageNumber, SaleID, Product, SaleDate, SalePrice
FROM
Sales_CTE
WHERE
PageNumber = @Targetpage
END