you can use the below solutionDECLARE @Temp table(father varchar(10),son varchar(10),lev int,fullpath varchar(1000))declare @lev intset @lev = 0INSERT @TempSELECT father,son,@lev,sonFROM YourTableWHERE son='D'while @@rowcount > 0begin set @lev = @lev + 1 INSERT @Temp (father,son, lev, fullpath) select t1.father, t1.son, @lev, t.fullpath + '.' + t1.son from @Temp t join YourTable t1 on t1.son = t.father and t.lev = @lev-1 left join @Temp x on x.father = t1.father and x.son = t1.son where x.father is nullendSELECT father,sonFROM @TempWHERE fullpath LIKE 'D%'
------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/