首页 > 代码库 > SqlServer 分页查询

SqlServer 分页查询

1.not in方法
select top 10 from books where id not in
(select top 30 id from books)
 
2.row_number()函数
select * from (
select *,row_number() over (order by id) num
from books) a where num between 31 and 40
 
3.存储过程分页
create proc proc_AdminPages
@pageIndex int,--当前页码
@pageSize int--每页数量
as
declare @begin int,@end int--定义局部变量
set @begin=@pageSize*(@pageIndex-1)+1--赋值
set @end=@pageSize*@pageIndex
select * from (
select ROW_NUMBER() over(order by id) as RowIndex,* from Admin) a
where RowIndex between @begin and @end
 
--执行
exec proc_AdminPages 2,1

SqlServer 分页查询