最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
SQL Server实现分页代码方法
时间:2022-06-29 07:42:24 编辑:袖梨 来源:一聚教程网
本篇文章小编给大家分享一下SQL Server实现分页代码方法,文章代码介绍的很详细,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。
一、创建测试表
CREATE TABLE [dbo].[Student](
[id] [int] NOT NULL,
[name] [nvarchar](50) NULL,
[age] [int] NULL)
二、创建测试数据
declare @i int
set @i=1
while(@i<10000)
begin
insert into Student select @i,left(newid(),7),@i+12
set @i += 1
end
三、测试
1、使用top关键字
top关键字表示跳过多少条取多少条
declare @pageCount int --每页条数 declare @pageNo int --页码 declare @startIndex int --跳过的条数 set @pageCount=10 set @pageNo=3 set @startIndex=(@pageCount*(@pageNo-1)) select top(@pageCount) * from Student where ID not in ( select top (@startIndex) ID from Student order by id ) order by ID
测试结果:
2、使用row_number()函数
declare @pageCount int --页数 declare @pageNo int --页码 set @pageCount=10 set @pageNo=3 --写法1:使用between and select t.row,* from ( select ROW_NUMBER() over(order by ID asc) as row,* from Student ) t where t.row between (@pageNo-1)*@pageCount+1 and @pageCount*@pageNo --写法2:使用 “>”运算符 select top (@pageCount) * from ( select ROW_NUMBER() over(order by ID asc) as row,* from Student ) t where t.row >(@pageNo-1)*@pageCount --写法3:使用and运算符 select top (@pageCount) * from ( select ROW_NUMBER() over(order by ID asc) as row,* from Student ) t where t.row >(@pageNo-1)*@pageCount and t.row<(@pageNo)*@pageCount+1
相关文章
- 我的常州怎么查社保卡余额 我的常州查询医保卡余额方法 05-06
- 天刀共鸣技能搭配攻略(掌握共鸣技能,提升天刀战斗实力) 05-06
- 《Neverness To Everness》周年庆委托任务在哪里介绍 05-06
- 纳米ai怎么修改资料 纳米ai修改资料方法 05-06
- 月蚀技能攻略(掌握月蚀技能,成为真正的黑暗之王!) 05-06
- 纳米ai怎么静音 纳米ai静音方法 05-06
