use a CTElike below;With PartChainAS(SELECT OLD_PART_NO,NEW_PART_NO, 1 AS Level,CAST(OLD_PART_NO AS varchar(max)) AS [Path]FROM TableWHERE NEW_PART_NO = @PartNoUNION ALLSELECT t.*,c.Level + 1,CAST(c.Path + '/' + t.OLD_PART_NO AS varchar(max))FROM PartChain cINNER JOIN Table tON t.NEW_PART_NO = c.OLD_PART_NO)SELECT *FROM PartChainORDER BY LEFT([Path],CHARINDEX('/',Path + '/')-1),Level DESCOPTION (MAXRECURSION 0)
------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs