SQL分页新语法

DECLARE @pageIndex INT;
DECLARE @pageSize INT;
SET @pageIndex = 0;
SET @pageSize = 10;
--传统写法
SELECT TOP (@pageSize) * FROM dbo.dt_order_goods 
WHERE dt_order_goods.id NOT IN (SELECT TOP (@pageIndex * @pageSize) dt_order_goods.id  FROM dbo.dt_order_goods ORDER BY dt_order_goods.id ASC)
ORDER BY dt_order_goods.id ASC
--新语法
SELECT * FROM dbo.dt_order_goods ORDER BY dt_order_goods.id ASC 
OFFSET (@pageIndex * @pageSize) ROWS
FETCH NEXT @pageSize ROWS ONLY

传统写法复杂臃肿,而且性能差,因为它做的是IN查询。从Sql2012开始就支持新语法分页了。代码简洁易懂,而且查询速度更快。

最后它们实现的效果都一样。强烈推荐新语法分页!~~

posted on 2018-03-08 16:18  潇潇@暮雨  阅读(518)  评论(0编辑  收藏  举报

导航