Click here to Skip to main content
15,890,825 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I have data as below and we are assuming parent node where Id=Parent_Id, so there are three top parents(1,5,7).

Id	Parent_Id	Title
1	1	Parent1
2	1	
3	2	
5	5	Parent2
6	5	
7	7	Parent3
8	7	
9	8	


I want to update result to all descendants of parent node and the result should be as below.

Id	Parent_Id	Title
1	1	Parent1
2	1	Parent1
3	2	Parent1
5	5	Parent2
6	5	Parent2
7	7	Parent3
8	7	Parent3
9	8	Parent3


What I have tried:

I am able to update parent to it's immediate child, so i am unable to update tile of level 3.

Please suggest how to proceed.
Posted
Updated 6-Mar-19 0:48am

1 solution

Your "level 3" items are not immediate children of the parent, so they will not update with the query.
Id	Parent_Id	Title
1	1	Parent1
2	1	// Child of 1 (Parent1)
3	2	// Child of 2, grandchild of 1 (Parent1)
5	5	Parent2
6	5	// Child of 5 (Parent2)
7	7	Parent3
8	7	// Child of 7 (Parent3)
9	8	// Child of 8, grandchild of 7 (Parent3)
You will need to run a second query for the grandchildren to match the parents, something like this should get you started
SQL
UPDATE t1
SET    t1.Title = t2.Title
FROM   HierarchyTable    t1
INNER JOIN HierarchTable t2 ON t1.Parent_Id = t2.Id
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900