Hi,
I have an interesting recursive update problem. I have a table that has parent/child row relationships
TableA (ID int, Parent Int, Department char(3)). There can be 1 to 10 sub-parents (think child / parent / grand parent/ etc....).
With data something like this
ID Parent Department
1 NULL ABC
2 1 NULL
3 1 NULL
4 NULL DEF
5 4 NULL
6 5 NULL
7 5 NULL
Note that ID 6 and 7 are children of 5 and 5 is a child of 4
The recursive update needs to take the Department value of the top most parent and store it in all the children under that parent. So the resulting data would be
ID Parent Department
1 NULL ABC
2 1 ABC
3 1 ABC
4 NULL DEF
5 4 DEF
6 5 DEF
7 5 DEF
This is an inherited problem and I have a While loop solution that simulates a recursive update, but was curious to see a CTE solution if possible. I've tried a couple different CTE's and keep running into a maximum recursion has been exhausted error...
Thanks