存储过程-数据分页

这个分页在200万数据下也只需12秒左右,在我的机上测试,AMD 1800XP 512M 120G西数
CREATE procedure seldata
(@pagesize int,
@pageindex int)
as
set nocount on
declare @RecordCount int
select @RecordCount=count(id) from test
if(@pageindex=1)
exec('select top '+@pagesize+'* from test order by id asc')
else
begin
declare @PageUpperBound int
declare @endrecords int
set @PageUpperBound=@pageindex*@pagesize
if(@PageUpperBound-@pagesize)>=@RecordCount
select ''
else if(@RecordCount-(@PageUpperBound-@pagesize)<=@pagesize)
begin
set @endrecords=@RecordCount-(@PageUpperBound-@pagesize)
exec('select * from (select top '+@endrecords+' * from test order by id)A order by id asc')
end
else
exec('select * from (select top '+@pagesize+'* from (select top '+@PageUpperBound+' * from test order by id asc)A order by id)B order by id asc')
end
set nocount off
go
posted @ 2006-01-22 17:28  致远钓客  阅读(162)  评论(0)    收藏  举报