SQL递归处理数据

/*
根据前置任务的时间,修改后续任务的时间
@ParentCyclePlanId:前置任务编号
*/
ALTER Proc ChangeCyclePlanDate
@ParentCyclePlanId int
As
Declare @CyclePlanId Int, @CyclePlanEndTime Char(10), @State Int, @Temp Char(10), @Other Char(10)

--声明游标
Declare CyclePlan_Cursor Cursor Local 
For
Select CyclePlanId, CyclePlanStartTime, CyclePlanEndTime From CyclePlan Where PrepositiveCyclePlan = @ParentCyclePlanId
For Update Of CyclePlanStartTime, CyclePlanEndTime

Open CyclePlan_Cursor
--如果没有后续任务则释放游标,并立即返回
If @@Cursor_Rows = 0
Begin
Close CyclePlan_Cursor
Deallocate CyclePlan_Cursor
Return
End
--递归
Select @CyclePlanEndTime = CyclePlanEndTime From CyclePlan Where CyclePlanId = @ParentCyclePlanId
Fetch CyclePlan_Cursor
Into @CyclePlanId, @Temp, @Other
Select @State = @@Fetch_Status

While @State = 0
Begin
Update CyclePlan Set
CyclePlanStartTime = replace(Convert(Char(10), DateAdd(day, 1, @CyclePlanEndTime), 111), '/', '-'),
CyclePlanEndTime = replace(Convert(Char(10), DateAdd(day, DateDiff(day, @Temp, CyclePlanEndTime) + 1, @CyclePlanEndTime), 111), '/', '-')
Where Current Of CyclePlan_Cursor
Exec ChangeCyclePlanDate @ParentCyclePlanId = @CyclePlanId
Fetch CyclePlan_Cursor
Into @CyclePlanId, @Temp
Select @State = @@Fetch_Status
End
Return
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值