当前位置:   article > 正文

sql游标的使用范例_sql 游标使用实例

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

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/619434
推荐阅读
相关标签
  

闽ICP备14008679号