row_number()函数是数据库中使用频率较高的函数,函数功能是给对查询出来的每一条记录生成一个序号(序号是从1开始递增)。它的主要应用是对查询的记录进行分页操作。
函数语法:select row_number() over(order by 列名) as 别名,* from 表名
下面俩如分别是未使用row_number()前和使用row_number()后的效果。
简单的分页程序(带参数的存储过程)如下:
create proc getPagesBook @index int
as
begin
select * from
(select row_number() over(order by 列名) as 别名 ,* from 表名) as temp_table
where temp_table.别名>(@index-1)*每页的记录数 and temp_table.别名<=@index*每页的记录数
end