[传智播客学习日记]分页查询的存储过程

 1 create proc usp_getPageData
2 @pageIndex int, --第几页
3 @pageSize int, --每页几条数据
4 @pageCount int output --输出总共有多少页
5 as
6 declare @count int --总数据条数
7 select @count=count(*) from classes
8 set pageCount=ceiling(@count*1.0/@pageSize) --得到页数(隐式转换为浮点数)
9 select * from
10 (select *, row_number() over(order by ___ desc) as num from ___) at ___
11 where num between (@pageIndex-1)*@pageSize+1 and @pageIndex*@pageSize
12 order by ___ desc

要实现这样的效果时使用这个存储过程:第一页显示1-5行,第二页6-10行...

其实完全可以用select top和not in来实现这个功能,这里用到的时row_number方法。

原文地址:https://www.cnblogs.com/Elijah/p/2259383.html