sql游标的使用范例

 

关于Sql游标的使用,请看下面的例子就什么都明白了。

declare @id nvarchar(20)  --定义变量来保存ID号
declare @A float                  --定义变量来保存值
declare mycursor cursor for select * from tb_c   --为所获得的数据集指定游标
open mycursor                   --打开游标
fetch next from mycursor  into @id,@A   --开始抓第一条数据
while(@@fetch_status=0)     --如果数据集里一直有数据
begin
 select  tb_b.name,(tb_b.gz + @A) from tb_b where  tb_b.id = @id   --开始做想做的事(什么更新呀,删除呀)
        fetch next from mycursor into @id,@A     --跳到下一条数据
end
close mycursor        --关闭游标
deallocate mycursor  --删除游标
 OpenActivity开发中使用的例子:
declare @EmployeeAdditionID bigint
declare @InventoryItemID bigint
declare @Duration nvarchar(50)
declare tmpcursor cursor for select * from EmployeeQualification where Duration is null and InventoryItemID is not null
open tmpcursor
fetch next from tmpcursor into @EmployeeAdditionID,@InventoryItemID,@Duration
while(@@fetch_status=0)
begin
select @Duration = Duration from InventoryItem where InventoryItemID = @InventoryItemID
update EmployeeQualification set Duration = @Duration where (EmployeeAdditionID = @EmployeeAdditionID) and (InventoryItemID = @InventoryItemID)
fetch next from tmpcursor into @employeeAdditionID,@InventoryItemID,@duration
end
close tmpcursor
deallocate tmpcursor

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值