sql游标的使用范例

来源:互联网 发布:淘宝广告法投诉处理 编辑:程序博客网 时间:2024/05/17 09:00

 

关于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

 

原创粉丝点击