分页的存储过程


/*分页存储过程*/
create proc usp_contacts_select_by_page
@pageindex int,---用户要查询的页码
@pagesize int,--每页几条
@pagecount int output,---一共查询了多少页
@recordcount int output---一共多少条数据


as
begin
---1.用户把要的数据查询出来
---1.1先把数据查询到临时表里面
select c1.*,c2.GroupName into #tmp_contacts from Contact as c1 inner join
ContactGroup as c2 on c1.GroupId=c2.GroupID

--1.2 再从临时表中 进行分页查询
select *
from (select *,rn=ROW_NUMBER() over (order by ContactID asc) from #tmp_contacts) as t

where t.rn between (@pageindex-1)*@pagesize+1 and @pageindex * @pagesize


---2.设置@pagecount 和 @recordcount的值
---计算总的记录数
set @recordcount=(select count(*) from #tmp_contacts)

---计算所有的页面
set @pagecount=CEILING(@recordcount*1.0/@pagesize)
end

原文地址:https://www.cnblogs.com/bingyizhihun/p/11427389.html