当前位置: > 数据库 > SQL Server 2005 >

sql server 2005中row_number实现分页的例子

时间:2014-09-08 21:37来源:linux.it.net.cn 作者:it
sql server 2005中row_number实现分页,分为二种方式,即使用between方式与top方式。

代码示例:

---row_number分页实例
---by http://www.it.net.cn
declare @PageNumber int
declare @PageSize int

set @PageNumber=2
set @PageSize=20

--使用between方式
select *
from
(
select
row_number() over (order by source_ip,id) as row_num,
* from tb_test_news) as page_table
where row_num between (@PageNumber - 1) * @PageSize + 1 and @PageNumber * @PageSize

--保用top方式
select top 20 *
from
(
select
row_number() over (order by source_ip,id) as row_num,
* from tb_test_news
) as page_table
where row_num > (@PageNumber - 1) * @PageSize
order by row_num


(责任编辑:IT)
------分隔线----------------------------