今天研究了一下row_number,用它来返回特定行的记录感觉是非常方便的,所以就做了个分页的存储过程,但不知道性能较之top和游标之类的那个好
代码
create procedure [dbo].[proc_TestPage]
--表名
@tablename nvarchar(255),
--排序字段
@sortcolumn nvarchar(255),
--每页记录数
@pagecount int,
--页号
@pageindex int
as
declare @beginrow int
declare @endrow int
set @beginrow=0
set @endrow=0
set @beginrow=(@pageindex-1)*@pagecount+1
set @endrow=@pageindex*@pagecount
declare @sqlstr nvarchar(4000)
set @sqlstr='with table1 as(select *,ROW_NUMBER() OVER(ORDER BY '+ @sortcolumn+' ) AS ROW'+
' FROM '+@tablename+') '
set @sqlstr=@sqlstr +' SELECT * FROM TABLE1 WHERE ROW BETWEEN '+cast(@beginrow as nvarchar )+' and '+cast(@endrow as nvarchar)
exec(@sqlstr)
--表名
@tablename nvarchar(255),
--排序字段
@sortcolumn nvarchar(255),
--每页记录数
@pagecount int,
--页号
@pageindex int
as
declare @beginrow int
declare @endrow int
set @beginrow=0
set @endrow=0
set @beginrow=(@pageindex-1)*@pagecount+1
set @endrow=@pageindex*@pagecount
declare @sqlstr nvarchar(4000)
set @sqlstr='with table1 as(select *,ROW_NUMBER() OVER(ORDER BY '+ @sortcolumn+' ) AS ROW'+
' FROM '+@tablename+') '
set @sqlstr=@sqlstr +' SELECT * FROM TABLE1 WHERE ROW BETWEEN '+cast(@beginrow as nvarchar )+' and '+cast(@endrow as nvarchar)
exec(@sqlstr)
使用的时候关键要注意ROW_NUMBER后面的OVER和Order BY,这个必须要有的啊,呵呵