码迷,mamicode.com
首页 > 其他好文 > 详细

使用OFFSET-FETCH进行数据过滤

时间:2015-03-08 22:44:49      阅读:224      评论:0      收藏:0      [点我收藏+]

标签:

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

使用OFFSET-FETCH进行数据过滤

标签:

原文地址:http://www.cnblogs.com/haseo/p/OFFSET-FETCH.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!