Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Building a 'path' in a hierarchy table

Author  Topic 

james_b
Starting Member

9 Posts

Posted - 2004-08-18 : 07:47:09
hi all, I've been trying to implement the process outlined by seventhknight in his article here:

http://www.seventhnight.com/treeStructs.asp

the main difference being that I need to store the values in one table. I'm getting stuck on step 3. I need the Sql to go from this:

Root_Id Parent_Node_Id Child_Node_Id Path

159    545    547    <Null>
159    545    548    <Null>
159    546    549    <Null>
159    547    550    <Null>
159    547    551    <Null>
159    547    552    <Null>
159    549    553    <Null>
159    546    548    <Null>
159    549    552    <Null>

To this:

Root_Id Parent_Node_Id Child_Node_Id Path

159    545    547    00159;00545;00547;
159    545    548    00159;00545;00548;
159    546    549    00159;00546;00549;
159    547    550    00159;00545;00547;00550;
159    547    551    00159;00545;00547;00551;
159    547    552    00159;00545;00547;00552;
159    549    553    00159;00546;00549;00553;
159    546    548    00159;00546;00548;
159    549    552    00159;00546;00549;00552;

Thanks for any help :)

James

Seventhnight
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2004-08-18 : 08:54:50
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 @myTable

Declare @tree table (pNodeId int, cNodeId int, processed bit default(0))
Insert Into @Tree
Select 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 @tree
Select pNodeId = 159, cNodeId = 545, Processed = 0
Union All
Select pNodeId = 159, cNodeId = 546, Processed = 0



Declare @pad nvarchar(100),
@lastCnt int
Set @Pad = '0000'

Declare @paths table (path nvarchar(1000), pNodeId int, cNodeId int)

Insert Into @paths
Select
path=right(@pad + convert(nvarchar,pNodeId),len(@pad))+';' + right(@pad + convert(nvarchar,cNodeId),len(@pad))+';',
pNodeId,
cNodeId
From @Tree
where pNodeId=159

Update A
Set Processed = 1
From @Tree as A
Inner Join @paths as B
On A.pNodeId = B.pNodeId
and A.cNodeId = B.cNodeId

While 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 = 0
End

Update A
Set Path = B.path
From @myTable as A
inner Join @paths as B
On A.parent_Node_Id = B.pNodeId
and A.child_Node_Id = B.cNodeId


Select * From @myTable


Corey
Go to Top of Page

james_b
Starting Member

9 Posts

Posted - 2004-08-18 : 10:50:04

Thanks Corey - that works great :)
Go to Top of Page

Seventhnight
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2004-08-18 : 10:52:44
I'm just excited someone is using my article!! Glad it works for you!

Corey
Go to Top of Page
   

- Advertisement -