如何更新SQL中的递归字段 [英] How to update recursive fields in SQL

查看:210
本文介绍了如何更新SQL中的递归字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我的表结构如下

TaskId INT

,ParentId INT FK

, Progress Float



样本数据

TaskId,ParentId,进度

1,NULL,0

2,1,0

3,2,0

4,2,0



如果我更新taskId = 4的进度,那么其父进程应该更新,例如TaskId = 2的进度,并且由于ParentId = 2的Parent是1,那么TaskId = 1的进度应该更新。



我创建了一个触发器,但它只更新了TaskId = 2的进度。



如何实现它?

Hi,
I have table structure like below
TaskId INT
,ParentId INT FK
,Progress Float

Sample data
TaskId,ParentId,Progress
1,NULL,0
2,1,0
3,2,0
4,2,0

If I update progress for the taskId = 4 then its parent's progress should update for example progress of TaskId=2 and since Parent of TaskId = 2 is 1 then progress of TaskId = 1 should get updated.

I have created a trigger but it only update the progress of TaskId = 2.

How can I achieve it ?

推荐答案

我是通过在触发器内使用公共表表达式处理递归调用来完成的。
I have done it by handling recursive call using common table expression inside trigger.


这篇关于如何更新SQL中的递归字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆