语句如下:
declare @page int declare @size int set @page=2 set @size=20 --第一种,优点,RowId可以作为行数,只扫描一次表数据,逻辑读取两次 SET STATISTICS PROFILE ON SET STATISTICS IO ON SET STATISTICS TIME ON /*--你的SQL脚本开始*/ select * from (select top (@page*@size) row_number() over(order by Id) RowId,* from T1 order by RowId) T where RowId between (@page*@size-@size+1) and (@page*@size)
--between 结果会包含边界值,所以需要+1 /*--你的SQL脚本结束*/ SET STATISTICS PROFILE OFF SET STATISTICS IO OFF SET STATISTICS TIME OFF --第二种 扫描两次表数据,逻辑读取四次 SET STATISTICS PROFILE ON SET STATISTICS IO ON SET STATISTICS TIME ON /*--你的SQL脚本开始*/ select top (@size) * from T1 where Id not in(select top (@page*@size-@size) ID from T1 order by Id)order by Id /*--你的SQL脚本结束*/ SET STATISTICS PROFILE OFF SET STATISTICS IO OFF SET STATISTICS TIME OFF
结果如下:
推荐使用第一种方式 分页