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
 Transact-SQL (2000)
 Get all descendents, ancestors ....

Author  Topic 

wgpubs
Yak Posting Veteran

67 Posts

Posted - 2005-03-04 : 14:36:08
Given the following table:

CategoryID INT
ParentCategoryID INT
CategoryName Varchar(20)

What is the most efficient way to traverse the hierarchy, both up and down, given a specific CategoryID?

For example, if I have the following
- Clothes
- Pants
- Long Pants
- Short Pants
- Really Short Pants
- Shirts

... and I started with the Pants node then I'd want to return
Pants, Long Pants, Short Pants and Really Short Pants as DESCENDANTS. If I started with Short Pants and wanted to get ANCESTORS ... I want returned Short Pants, Pants, Clothes.

Thanks - wg

Seventhnight
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2005-03-04 : 14:44:19
take a read through: http://www.seventhnight.com/treestructs.asp

Corey

"If the only tool you have is a hammer, the whole world looks like a nail." - Mark Twain
Go to Top of Page
   

- Advertisement -