Allright...Part of your problem is that you didn't include all the information in your parent and child columns. You need to identify how 159 comes into play. I copied the sql script from my website and made a few changes:(I tried to bold green the changes I had to make.)Declare @myTable table (Root_Id int, Parent_Node_Id int, Child_Node_Id int, Path varchar(100))Insert Into @myTable Values (159,545,547,Null)Insert Into @myTable Values (159,545,548,Null)Insert Into @myTable Values (159,546,549,Null)Insert Into @myTable Values (159,547,550,Null)Insert Into @myTable Values (159,547,551,Null)Insert Into @myTable Values (159,547,552,Null)Insert Into @myTable Values (159,549,553,Null)Insert Into @myTable Values (159,546,548,Null)Insert Into @myTable Values (159,549,552,Null)Select * From @myTableDeclare @tree table (pNodeId int, cNodeId int, processed bit default(0))Insert Into @TreeSelect pNodeId = parent_Node_Id, cNodeId = child_node_Id, processed = 0 From @myTable/*****************************************You didn't have a starting point so I had to add it.********************************************/Insert Into @treeSelect pNodeId = 159, cNodeId = 545, Processed = 0Union AllSelect pNodeId = 159, cNodeId = 546, Processed = 0Declare @pad nvarchar(100), @lastCnt intSet @Pad = '0000'Declare @paths table (path nvarchar(1000), pNodeId int, cNodeId int)Insert Into @pathsSelect path=right(@pad + convert(nvarchar,pNodeId),len(@pad))+';' + right(@pad + convert(nvarchar,cNodeId),len(@pad))+';', pNodeId, cNodeIdFrom @Treewhere pNodeId=159Update ASet Processed = 1From @Tree as AInner Join @paths as BOn A.pNodeId = B.pNodeIdand A.cNodeId = B.cNodeIdWhile exists(Select * From @tree Where Processed = 0)Begin Insert Into @paths Select path=path + case when B.cNodeId is not null then right(@pad + convert(nvarchar,B.cNodeId),len(@pad))+';' else '' end, B.pNodeId, B.cNodeId From @Paths as A Left Join @Tree as B On A.cNodeId = B.pNodeId Where B.Processed = 0 Update A Set Processed = 1 From @Tree as A Inner Join @paths as B On A.pNodeId = B.pNodeId and A.cNodeId = B.cNodeId Where A.Processed = 0EndUpdate ASet Path = B.path From @myTable as Ainner Join @paths as BOn A.parent_Node_Id = B.pNodeIdand A.child_Node_Id = B.cNodeIdSelect * From @myTable
Corey