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)
 Hierarchical data in resultset

Author  Topic 

juliano.net
Starting Member

10 Posts

Posted - 2006-04-30 : 23:30:34
How can I create a function that returns hierarchical data from a table with this structure:

- CategoryID
- CategoryName
- CategoryFather

I want to bring the result set like this...

CategoryID | CategoryName | CategoryFather | HierarchicalLevel
1 | Video | 0 | 0
2 | DivX | 1 | 1
3 | WMV | 1 | 1
4 | Programming | 0 | 0
5 | Web | 4 | 1
6 | ASP.Net | 5 | 2
7 | ColdFusion | 5 | 2


How can I do this? Does anybody has a sample code? I need this on SQL Server 2000 and if it's possible (but not too necessary) in SQL Server 2005.

Thanks.

[]'s
Juliano
.Net Developer

darinh
Yak Posting Veteran

58 Posts

Posted - 2006-05-01 : 01:13:54
Search for recursive joins or look at http://www.sqlteam.com/item.asp?ItemID=8866
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2006-05-01 : 10:47:27
SQL 2005 makes this very easy.

http://blogs.conchango.com/christianwade/archive/2004/11/09/234.aspx

Do more googling for more examples.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-05-02 : 02:38:51
Other method in sql server 2000
http://www.nigelrivett.net/RetrieveTreeHierarchy.html

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

sshelper
Posting Yak Master

216 Posts

Posted - 2006-05-02 : 09:52:02
Here's another link which is a user-defined function that will return the tree node level for a hierarchical structure:

http://www.sql-server-helper.com/functions/get-tree-node-level.aspx

SQL Server Helper
http://www.sql-server-helper.com
Go to Top of Page
   

- Advertisement -