I have following two tables
Table Person
Id Name
1 A
2 B
3 C
4 D
5 E
Table RelationHierarchy
Another way to do this in a loop if you wanted to work with sets is:
SELECT *
INTO #parentchild
from RelationHierarchy
WHILE EXISTS
(select 1 from #parentchild A inner join #parentchild B on A.ChildID = B.ParentID Where A.ParentID <> B.ParentID )
BEGIN
update B set B.ParentID = A.ParentID
from #parentchild A inner join #parentchild B on A.ChildID = B.ParentID
END
select * from #parentchild