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)
 Getting Sub Tree Nodes

Author  Topic 

maximus_vj
Yak Posting Veteran

88 Posts

Posted - 2004-12-09 : 05:39:58
Hello Everyone,
I have a tree structure in a table and I want to get the whole subtree given a node.

This is how my table looks:

Node_ID----ParentNodeID----ChildCount
-------------------------------------
100-------------0---------------2
101-------------100-------------3
102-------------100-------------2
103-------------101-------------0
104-------------101-------------0
105-------------101-------------0
106-------------102-------------0
107-------------102-------------0

As you can see in this Node ID is the ID for the Node. ParentNodeID is the Node_ID of the parent. If ParentNodeID is 0 that means that it is the root node. And if the ChildCount is 0 it is the leaf node.

The depth of the tree is not fixed.

How do I write a SQL Query so that if I supply 100 I will get the whole sub tree from 100 i.e. all the above nodes. And if I supply
101 I will get 101,103,104,105. Similarly if I give 102 I should get 102,106,107.

Thanks for your help,
maximus

Seventhnight
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2004-12-09 : 07:34:48
try reading through this: http://www.seventhnight.com/treestructs.asp

The second or third part has similar type queries...

Corey
Go to Top of Page

maximus_vj
Yak Posting Veteran

88 Posts

Posted - 2004-12-10 : 00:28:20
Thnx Corey !!!

maximus
Go to Top of Page
   

- Advertisement -