sqlserver 2012开窗函数快速分页

开工,建表插入值

create table test(
  keyid int identity,
  sort varchar(10),
  qty int
) 

插入数据,特意删除了一个id为 7的数据,让ID不连续!
在这里插入图片描述
接下来,分页sql

select COUNT(1) OVER () as [总数],* from test 
order by keyid offset 0 rows fetch next 7 rows only

在这里插入图片描述
注意 0 这里是更具ID的偏移量来检索的,实际就是索引值,从0开始与id真实值无关!
fetch next 7 rows only表示会从偏移量后面去7调数据

那么最终写写法就是

select COUNT(1) OVER () as [总数],* from test 
order by keyid offset   每页数量 * (页码-1)    rows fetch next  每页数量  rows only
//这样就可以了

欢迎指点!

猜你喜欢

转载自blog.csdn.net/weixin_42780928/article/details/91954951