WEB开发网
开发学院数据库MSSQL Server 如何实现SQL Server 2005快速web分页 阅读

如何实现SQL Server 2005快速web分页

 2007-05-15 09:27:34 来源:WEB开发网   
核心提示: CODE:列表ADECLARE @i SMALLINTSET @i = 1WHILE (@i <=100)BEGININSERT INTO SalesHistory(Product, SaleDate, SalePrice)VALUES('Computer', DAT

CODE:

列表A

DECLARE @i SMALLINT
SET @i = 1
WHILE (@i <=100)
BEGIN   
INSERT INTO SalesHistory   
(Product, SaleDate, SalePrice)   
VALUES   
('Computer', DATEADD(mm, @i, '3/11/1919'), DATEPART(ms, GETDATE()) + (@i + 57) )
INSERT INTO SalesHistory   
(Product, SaleDate, SalePrice)   
VALUES   
('BigScreen', DATEADD(mm, @i, '3/11/1927'), DATEPART(ms, GETDATE()) + (@i + 13) )
INSERT INTO SalesHistory   
(Product, SaleDate, SalePrice)   
VALUES   
('PoolTable', DATEADD(mm, @i, '3/11/1908'), DATEPART(ms, GETDATE()) + (@i + 29) )
SET @i = @i + 1
END

现在数据表中已经具有实例数据。接下来我们看看如何调用程序来实现数据的分页显示。列表B包含这个程序的脚本内容。这个程序含有两个参数:1.页面大小(给定页面要显示的数据记录数目)2.目标页面(返回该页的数据记录)。

CODE:

列表B

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

如果你刚刚开始使用SQL Server,可能会不熟悉以“WITH”开头的声明语句。这条语句会调用SQL Server中的一个新属性,我们称之为common table expression(CTE),从本质上来说,我们可以将CTE看作是高版本的临时表。

Tags:如何 实现 SQL

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接