使用游标编写的存储过程进行分页

思路:采用游标定位于所需页面的每一条记录的位置,再循环获取此页面的其它记录。


--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 来处理每个记录集,将所有记录集添加到一个新的数据容器里

至于存储过程在程序中的调用就不说了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值