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 

This query selects all children nodes in a hierarchical tree.

What I need to do is, with all the results returned from the query above, is update a field called [level] by an increment of 1.

I have tried myself with a few permutations but I get errors about not being able to update a derived table

OzrenTkalcecKrznaric
  • 5,535
  • 4
  • 34
  • 57
totalitarian
  • 3,606
  • 6
  • 32
  • 55

1 Answers1

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
        )
Andomar
  • 232,371
  • 49
  • 380
  • 404