with T1 as
( select tree.* from tree where parent_id = 2
union all
select tree.* from tree
join T1 on (tree.parent_id=T1.id)
)
select * from T1
此查询选择分层树中的所有子节点。
我需要做的是,从上面的查询中返回的所有结果,都是以1的增量更新名为[level]的字段。
我尝试了一些排列,但是我发现无法更新派生表的错误
答案 0 :(得分:2)
; with T1 as
(
select tree.*
from tree
where parent_id = 2
union all
select tree.*
from tree
join T1
on tree.parent_id=T1.id
)
update tree
set level = level + 1
where id in
(
select id
from t1
)