TOP的工业标准版 OFFSET-FETCH
OFFSET 用来设置跳过行的数量 FETCH 用来设置检索多少行,必须要排序才能用,SQL Server 2012的新语法
从语意的角度来讲如果要跳开几行,那么你用关键词 FETCH NEXT,如果不跳过任何行。则使用FETCH FIRST 。(也就是说这两个是一个意思? 有其他区分请告诉我。。)
-- skip 50 rows, fetch next 25 rows SELECT orderid, orderdate, custid, empid FROM Sales.Orders ORDER BY orderdate DESC, orderid DESC OFFSET 50 ROWS FETCH NEXT 25 ROWS ONLY; -- fetch first 25 rows SELECT orderid, orderdate, custid, empid FROM Sales.Orders ORDER BY orderdate DESC, orderid DESC OFFSET 0 ROWS FETCH FIRST 25 ROWS ONLY; -- skip 50 rows, return all the rest SELECT orderid, orderdate, custid, empid FROM Sales.Orders ORDER BY orderdate DESC, orderid DESC OFFSET 50 ROWS; -- ORDER BY is mandatory; return some 3 rows SELECT orderid, orderdate, custid, empid FROM Sales.Orders ORDER BY (SELECT NULL) OFFSET 0 ROWS FETCH FIRST 3 ROWS ONLY; GO -- 分页 DECLARE @pagesize AS BIGINT = 25, @pagenum AS BIGINT = 3; SELECT orderid, orderdate, custid, empid FROM Sales.Orders ORDER BY orderdate DESC, orderid DESC OFFSET (@pagenum - 1) * @pagesize ROWS FETCH NEXT @pagesize ROWS ONLY; GO