思路:采用游标定位于所需页面的每一条记录的位置,再循环获取此页面的其它记录。
--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 来处理每个记录集,将所有记录集添加到一个新的数据容器里
至于存储过程在程序中的调用就不说了
博客介绍了采用游标定位于所需页面记录位置,循环获取页面其它记录的思路,给出了相应存储过程代码。指出使用游标的好处是可跳到首条记录提取所需记录,缺点是返回多记录集且每条仅含一条所需记录,还给出用DateSet或DataReader处理的解决方案。

1631

被折叠的 条评论
为什么被折叠?



