sql server 分页
测试数据脚本
create database DBTest use DBTest --创建测试表 create table pagetest ( id int identity(1,1) not null, col01 int null, col02 nvarchar(50) null, col03 datetime null ) declare @i int set @i=0 while(@i<10000) begin insert into pagetest select cast(floor(rand()*10000) as int),left(newid(),10),getdate() set @i=@i+1 end
声明变量
declare @pageNum int=50 declare @page int =199
分页之效率最高
SELECT TOP (@pageNum) * FROM pagetest WHERE id NOT IN ( SELECT TOP (@pageNum*(@page-1)) id FROM pagetest ORDER BY id ) ORDER BY id
分页之最常用
SELECT TOP(@pageNum) * FROM ( SELECT ROW_NUMBER() OVER (ORDER BY id) AS RowNumber,* FROM pagetest ) A WHERE RowNumber > (@pageNum*(@page-1))