传说中的分页3

本文介绍了两种使用SQL创建分页存储过程的方法。一种适用于SQL Server 2000,通过定义表变量并结合行计数实现分页;另一种适用于SQL Server 2005及以上版本,采用窗口函数ROW_NUMBER()简化分页逻辑。
摘要由CSDN通过智能技术生成

首先获得所有的记录集合的存储过程:

create   PROCEDURE [dbo].[P_GetOrderNumber]
AS
 select count(orderid) from orders;----orders
为表
 RETURN

分页的存储过程

create  procedure [dbo].[P_GetPagedOrders2000]
(@startIndex int,  ---
开始页数
@pageSize int----
每一页显示的数目
)
as
set nocount on
declare @indextable table(id int identity(1,1),nid int)  ----
定义一个表变量
declare @PageUpperBound int
set @PageUpperBound=@startIndex+@pagesize-1
set rowcount @PageUpperBound
insert into @indextable(nid) select orderid from orders order by orderid desc
select O.orderid,O.orderdate,O.customerid,C.CompanyName,E.FirstName+' '+E.LastName as EmployeeName
from orders O
left outer join Customers C
on O.CustomerID=C.CustomerID
left outer join Employees E
on O.EmployeeID=E.EmployeeID
inner join @indextable t on
O.orderid=t.nid
where t.id between @startIndex and @PageUpperBound order by t.id    ----
实现分页的关键
set nocount off


2005:

create  [dbo].[P_GetPagedOrders2005]
(@startIndex INT,
 @pageSize INT
 )
AS
begin
WITH orderList AS (
SELECT ROW_NUMBER() OVER (ORDER BY O.orderid DESC)AS Row, O.orderid,O.orderdate,O.customerid,C.CompanyName,E.FirstName+' '+E.LastName as EmployeeName
from orders O
left outer join Customers C
on O.CustomerID=C.CustomerID
left outer join Employees E
on O.EmployeeID=E.EmployeeID)

SELECT orderid,orderdate,customerid,companyName,employeeName
FROM orderlist
WHERE Row between @startIndex and @startIndex+@pageSize-1
end

border="0" name="I1" align="middle" src="/mb/GGtgDB.html" frameborder="0" width="480" scrolling="no" height="75" valign="top">  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值