SQL 分页

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;

 

posted @ 2016-01-03 21:40  大胡子毛绒老头  阅读(129)  评论(0编辑  收藏  举报