SQL Server 2005 的新特性Row_Number()函数
语法:ROW_NUMBER() OVER(PARTITION BY COLUMN ORDER BY COLUMN)
例子:
select * from ( select *, ROW_NUMBER() OVER(Order by a.CreateTime DESC ) AS RowNumber from table_name as a ) as b where RowNumber BETWEEN 1 and 5
将会返回table表
其中有一列名字为 RowNumber, 编号从1开始
示例:
xlh row_num
1700 1
1500 2
1085 3
710 4
有了row_num 编号之后是不是很方便分页呀! 哈哈
只要使用
where RowNumber between
就可以实现分页了 呵呵(从此分页就是这么简单)
例子:
select *
from(
select ROW_NUMBER() OVER( ORDER BY PSIO.CreateTime DESC ) AS RowNumber,PSIO.SeqNo,PSIO.CreateTime from dbo.Output PSIO
inner join Album PPA on PSIO.PPAID=PPA.PPAID
where PPA.PPAID=103--PPAID=3.PPAID
) T where RowNumber BETWEEN 1 and 5 order by 1
在当前select里面不能采用 RowNumber字段,并且不能使用排序