欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

sqlserver分页的两种写法分别介绍

程序员文章站 2023-11-29 10:02:58
第一种是最传统的写法,用存储过程中的变量作为分页的乘数 复制代码 代码如下: [c-sharp] view plaincopyprint?create proc p_pag...
第一种是最传统的写法,用存储过程中的变量作为分页的乘数
复制代码 代码如下:

[c-sharp] view plaincopyprint?create proc p_paged1
@pagesize int,@currentpage int
as
select top (@pagesize) * from student
where id not in
(select top (@pagesize*(@currentpage-1)) id from student)
go
exec p_paged1 2,3
create proc p_paged1
@pagesize int,@currentpage int
as
select top (@pagesize) * from student
where id not in
(select top (@pagesize*(@currentpage-1)) id from student)
go
exec p_paged1 2,3

--sql server2005以后的分页语句
复制代码 代码如下:

[c-sharp] view plaincopyprint?create proc p_paged2
@pagestart int, @pageend int
as
select * from
(select *,row_number() over(order by id desc) as rnum
from student) t
where t.rnum between @pagestart and @pageend
go

exec p_paged2 5,10