思路:采用游标定位于所需页面的每一条记录的位置,再循环获取此页面的其它记录。
--PageIndex 为所要获取的页面的索引号,PageSize为每页显示的记录数
create procedure FetchPage(@PageIndex smallint,@PageSize smallint)
as
declare @index smallint
declare @FirstRecord smallint
--设置要获取的页面的第一条记录位置
set @FirstRecord=(@PageIndex-1)*@PageSize+1
--关联游标
declare customer_cursor scroll cursor for
select CustomerID,CompanyName,ContactName,Address from Customers Order by CompanyName
open customer_cursor
fetch absolute @FirstRecord from customer_cursor
--循环获取剩余记录
set @index =1
while @@FETCH_STATUS=0 and @index<@PageSize begin
fetch next from customer_cursor
set @index=@index+1
end
close customer_cursor
deallocate customer_cursor
使用游标的好处是可以跳到想要获取页面的第一条记录并提取需要的记录,缺点是会同时返回多个记录集,而每个记录集却只包含一条
需要的记录。
解决方案:可以采用DateSet 或DataReader 来处理每个记录集,将所有记录集添加到一个新的数据容器里
至于存储过程在程序中的调用就不说了