程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> SqlServer數據庫 >> 關於SqlServer >> SQL Server實現分頁的方式

SQL Server實現分頁的方式

編輯:關於SqlServer

2000:

首先獲得所有的記錄集合的存儲過程:

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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved