码迷,mamicode.com
首页 > 数据库 > 详细

SQL 分页

时间:2016-01-03 22:35:20      阅读:312      评论:0      收藏:0      [点我收藏+]

标签:

declare @page int;
declare @size int;
set @page = 4;
set @size = 4;
declare @offset int;
set @offset = (@page * @size - @page);
/*offset*/
select * from Products order by ProductId offset @offset rows fetch next @size rows only;
/*row_number/返回标识字段*/
select top (@size) ProductID from (
        select row_number() over (order by ProductId) as rn, ProductID from Products
    ) as t where t.rn > @offset;
/*page*size/返回ProductID/返回倒序的标识字段/注意两个order by 应一致,如果order by字段不是标识字段,子查询的select中应加入order by字段*/
select top (@size) ProductID from (
        select top (@page * @size) ProductID from Products order by ProductID
    ) as a order by a.ProductID desc;

 

SQL 分页

标签:

原文地址:http://www.cnblogs.com/ly45/p/5097260.html

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