1
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 

此查询选择层次树中的所有子节点。

我需要做的是,根据上面查询返回的所有结果,将一个名为 [level] 的字段更新为 1。

我已经尝试了一些排列,但我收到关于无法更新派生表的错误

4

1 回答 1

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
        )
于 2013-07-27T09:24:53.827 回答