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)
 help with recursion

Author  Topic 

edp33
Starting Member

9 Posts

Posted - 2007-03-09 : 06:40:03
I am trying to traverse a tree structure like below,

1 Pets
--1.1 Cat
----1.1.1 Persian
----1.1.2 Bengal
--1.2 Dog
----1.2.1 Poodle
2 etc

I would like to be able to search by a keyword, i.e. Poodle, or the reference number, i.e. 1.2.1. I would prefer to do this all through a stored procedure if possible, it seems recursion is the way to go as the number of levels may increase in the future but i'm completely new to this. From what i've seen so far I would need a table structure with a parentID,NodeID,Name field and Primary key.

i.e,

ID...Name......Parent.....NodeID
1....Pets.........0............1
2....Cat..........1............1
3....Dog..........1............1
4....Persian.....2............1
5....Bengal......2............2
6....Poodle......3............1
etc

i've heard that SQL SERVER 2005 provides recursion through CTE, is this the recommended way/only way to achieve this?

Any tips on where to start would be really appreciated.









jen
Master Smack Fu Yak Hacker

4110 Posts

Posted - 2007-03-09 : 09:28:13
this has been discussed before and probably posted on the weblogs
do a search over there

good luck!

--------------------
keeping it simple...
Go to Top of Page

blindman
Master Smack Fu Yak Hacker

2365 Posts

Posted - 2007-03-09 : 11:07:34
TSQL supports recursion, but avoid using it as it is ineffecient and is limited to 32 levels.

Check out this article on a method of navigating hierarchies:
http://sqlblindman.googlepages.com/returningchildrecords


e4 d5 xd5 Nf6
Go to Top of Page
   

- Advertisement -