CREATE INDEX idx_od_oid_i_cid_eid ON Sales.Orders(orderdate, orderid) INCLUDE(custid, empid);
--orderid是sales.Orders的主键,排序列是orderdate,决胜属性是orderid
---------------------------------
CREATE PROC dbo.GetNextPage
@anchor AS INT, -- 上次分页后最后一行的主键
@n AS INT = 10
AS
SELECT TOP (@n) O.orderid, O.orderdate, O.custid, O.empid
FROM Sales.Orders AS O
JOIN Sales.Orders AS A
ON A.orderid = @anchor
AND (O.orderdate >= A.orderdate
And (O.orderdate > A.orderdate --
Or O.orderid > A.orderid))
ORDER BY O.orderdate, O.orderid;
GO
-------------------------
Alter PROC dbo.GetNextPage
@anchor AS INT, -- key of last row in prev page
@n AS INT = 10
AS
SELECT TOP (@n) O.orderid, O.orderdate, O.custid, O.empid
FROM Sales.Orders AS O
Where
O.orderid > @anchor
ORDER BY O.orderdate, O.orderid;
GO
--运行这个测试,测试结果是一样的。
EXEC dbo.GetNextPage @anchor = 10257;