sqlserver存取过程-游标

本文介绍了一种使用T-SQL游标和批处理来更新数据库中特定记录的方法。通过定义游标来遍历满足条件的数据,并对每条记录进行逐一批处理更新,确保了数据的一致性和准确性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ALTER proc [dbo].[common_proc_temp2]

as  begin

declare @id varchar(50);
declare @cbcontractid varchar(50);
declare @xh int ;
declare owner_03_cursor cursor scroll
for select id from cb_contract where canton_id like '20%'
--2.打开游标
open owner_03_cursor
fetch next from owner_03_cursor into @cbcontractid --into的变量数量必须与游标查询结果集的列数相同
while @@fetch_status=0 --提取成功,进行下一条数据的提取操作
begin
set @xh=1
declare owner_04_cursor cursor scroll
for select id from cb_contract_change where f_cbcontractid=@cbcontractid order by bgrq asc
open owner_04_cursor
fetch next from owner_04_cursor into @id
while @@fetch_status=0
begin
update cb_contract_change set xh=@xh where id=@id
set @xh=@xh+1;
fetch next from owner_04_cursor into @id
end
close owner_04_cursor;
deallocate owner_04_cursor;

fetch next from owner_03_cursor into @cbcontractid --移动游标
end
close owner_03_cursor;
deallocate owner_03_cursor;
end;

转载于:https://www.cnblogs.com/meisp/p/5598904.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值