结合row_number函数和临时表进行分页的思路

 

//先在表中具体化所有页,并创建行号rownum, 然后在行号上创建索引

SELECT  row_number() over(order by id desc) as rownum, *
INTO  #temp
FROM  dbo.Sales

 

Create  Unique  Clustered  Index  idx_row  ON  #temp(rownum);

 

 

//对临时表进行分页查询
declare @pagesize as int , @pagenum  as int;
set @pagesize = 5;
set @pagenum = 2;

 

select * from #temp
where rownum between @pagesize*(@pagenum-1) and @pagesize*pagenum
order by rownum

 

//删除临时表
drop  table  #temp

你可能感兴趣的:(table)