检索数据库中指定页的记录(SqlServer 2000示例)

些存贮过程能检索数据库中指定页的记录,页面大小可由自己指定,能适合用于需要分页显示的场合,比较灵活。(源于《Fast Track ASP.Net with C#》一书)

CREATE PROCEDURE sp_GetEmployeesByPage
@PageNumber?int,
@PageSize?int
AS

-- create a temporary table with the columns we are interested in
CREATE TABLE #TempEmployees
(
?ID ??int IDENTITY PRIMARY KEY,
?EmployeeID?int,
?LastName?nvarchar(20),
?FirstName?nvarchar(10),
?Title??nvarchar(30),
?TitleOfCourtesy?nvarchar(25),
?Address??nvarchar(60),
?City??nvarchar(15),
?Region??nvarchar(15),
?Country??nvarchar(15),
?Notes??ntext
)

-- fill the temp table with all the employees
INSERT INTO #TempEmployees
(
?EmployeeID,
?LastName,
?FirstName,
?Title,
?TitleOfCourtesy,
?Address,
?City,
?Region,
?Country,
?Notes
)
SELECT
?EmployeeID,
?LastName,
?FirstName,
?Title,
?TitleOfCourtesy,
?Address,
?City,
?Region,
?Country,
?Notes
FROM
? Employees ORDER BY EmployeeID ASC

-- declare two variables to calculate the range of records to extract for the specified page
DECLARE @FromID int
DECLARE @ToID int
-- calculate the first and last ID of the range of records we need
SET @FromID = ((@PageNumber - 1) * @PageSize) + 1
SET @ToID = @PageNumber * @PageSize

-- select the page of records
SELECT * FROM #TempEmployees WHERE ID >= @FromID AND ID <= @ToID
GO

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值