AS an exampleCREATE TABLE #Temp--create sample table to hold data(OptionItemId int,ParentId int, LookupItemId int, DataTypeId int, SectionId int)INSERT #Temp--create sample dataSELECT 1 AS OptionItemId,NULL AS ParentId, 1 AS LookupItemId, 3 AS DataTypeId, 1 AS SectionId union all SELECT 2 , 1 , 2 , 4 , 1 union all SELECT 3 , 2 , 3 , 1 , 1 union all SELECT 4 , 1 , 4 , 3 , 0 union all SELECT 5 , 2 , 5 , 1 , 1 union all SELECT 6 , 5 , 6 , 1 , 1 union all SELECT 7 , 6 , 5 , 1 , 2 union all SELECT 8 , 1 , 8 , 3 , 0 union all SELECT 9 , 8 , 3 , 1 , 1 union all SELECT 10 , 8 , 6 , 1 , 8 union all SELECT 11 , 2 , 4 , 1 , 1 union all SELECT 12 , 1 , 7 , 3 , 4 union all SELECT 13 , 11 , 1 , 2 , 1 union all SELECT 14 , 2 , 2 , 1 , 1 union all SELECT 15 , 14 , 5 , 2 , 1 union all SELECT 16 , 2 , 3 , 2 , 2 union all SELECT 17 , 16 , 1 , 2 , 2 DECLARE @OptionItemId int,@sectionid intSELECT @OptionItemId=8,@sectionid=10SELECT * FROM #TEmp--data before update;With CTE (OptionItemId, ParentId, LookupItemId, DataTypeId, SectionId ) AS(SELECT OptionItemId, ParentId, LookupItemId, DataTypeId, SectionIdFROM #TempWHERE OptionItemId=@OptionItemIdUNION ALLSELECT t.OptionItemId, t.ParentId, t.LookupItemId, t.DataTypeId, t.SectionIdFROM CTE cJOIN #Temp tON t.ParentId=c.OptionItemId)UPDATE tSET t.SectionId=@sectionidFROM #Temp tJOIN CTE cON c.OptionItemId=t.OptionItemIdAND c.OptionItemId<>@OptionItemIdSELECT * FROM #TEmp--data after updateDROP TABLE #Temp--clean up,remove used temp tableoutput------------------------------before update-------------------------------OptionItemId ParentId LookupItemId DataTypeId SectionId------------ ----------- ------------ ----------- -----------1 NULL 1 3 12 1 2 4 13 2 3 1 14 1 4 3 05 2 5 1 16 5 6 1 17 6 5 1 28 1 8 3 09 8 3 1 110 8 6 1 811 2 4 1 112 1 7 3 413 11 1 2 114 2 2 1 115 14 5 2 116 2 3 2 217 16 1 2 2after update-----------------------------------------OptionItemId ParentId LookupItemId DataTypeId SectionId------------ ----------- ------------ ----------- -----------1 NULL 1 3 12 1 2 4 13 2 3 1 14 1 4 3 05 2 5 1 16 5 6 1 17 6 5 1 28 1 8 3 09 8 3 1 1010 8 6 1 1011 2 4 1 112 1 7 3 413 11 1 2 114 2 2 1 115 14 5 2 116 2 3 2 217 16 1 2 2