sqlserver 分页存储过程
--分頁
--求? 共多少頁pageCount?? 當(dāng)前頁的數(shù)據(jù)
--已知? 1 每頁顯示幾條?? pageSize??? 2 當(dāng)前頁碼 pageIndex
select 6/3
select CEILING( 7.0/3)
--第一頁??? pageSize=3
select * from
(select *,ROW_NUMBER() over(order by ptime desc) as num from photos) as t
where num between 1 and 3 order by? ptime desc
--第二頁
select * from
(select *,ROW_NUMBER() over(order by ptime desc) as num from photos) as t
where num between 4 and 6 order by? ptime desc
--第三頁
select * from
(select *,ROW_NUMBER() over(order by ptime desc) as num from photos) as t
where num between 7 and 9 order by? ptime desc
--第pageIndex頁? pageSize=3
select * from
(select *,ROW_NUMBER() over(order by ptime desc) as num from photos) as t
where num between (pageIndex-1)*pageSize+1 and pageIndex*pageSize order by? ptime desc
--分頁的存儲過程? ?
create proc usp_photos
?? ?@pageIndex int,?? ??? ?--當(dāng)前頁碼
?? ?@pageSize int,?? ??? ?--頁容量
?? ?@pageCount int output? --共多少頁 輸出參數(shù)
as
?? ?declare @count int
?? ?select @count=COUNT(*) from Photos
?? ?set @pageCount = CEILING( @count*1.0/@pageSize)
?? ?select * from
(select *,ROW_NUMBER() over(order by ptime desc) as num from photos) as t
where num between (@pageIndex-1)*@pageSize+1 and @pageIndex*@pageSize order by? ptime desc
--測試存儲過程
declare @n int
exec usp_photos 2,2,@n output
print @n
use myphotos?? ?
select * from PhotoType
轉(zhuǎn)載于:https://www.cnblogs.com/eric-gms/p/3464876.html
總結(jié)
以上是生活随笔為你收集整理的sqlserver 分页存储过程的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: display与visibility区别
- 下一篇: ASP.NET MVC 5 学习教程:D