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 に答える