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

网络编程 2021-07-05 13:42www.168986.cn编程入门
本文将介绍sql server传统的写法与SQL Server2005以后的分页语句需要了解的朋友可以参考下
第一种是最传统的写法,用存储过程中的变量作为分页的乘数
代码如下:

[c-sharp] view plaincopyprint?create proc p_paged1
@pageSize int,@currentPage int
as
select (@pageSize) from student
where id not in
(select (@pageSize(@currentPage-1)) id from student)
go
exec p_paged1 2,3
create proc p_paged1
@pageSize int,@currentPage int
as
select (@pageSize) from student
where id not in
(select (@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

Copyright © 2016-2025 www.168986.cn 狼蚁网络 版权所有 Power by