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)
 Selecting a category tree

Author  Topic 

Rogz
Starting Member

2 Posts

Posted - 2006-01-09 : 11:16:04
Hi there

I'm trying to return a result set for use with a category tree. I also want to return the results in a way the the parent cateogories are returned in alphabetical order...so category 1 and all its children and then category 2 and all its children and so on. Am i best off to select all the parent categories into a cursor and then for each parent category insert it into temp table and then selecting all its children into the temp table as well? The following is how the data currently exists in the table

CategoryID Parent CategoryID Category
65 NULL Economic Development
66 65 A Wealth of Opportunity
67 NULL Top Category
69 67 test 3
73 NULL A new category
74 73 test
75 73 a test

Vivaldi
Constraint Violating Yak Guru

298 Posts

Posted - 2006-01-09 : 11:41:21
The word "best" and the word "cursor" seldom can be used in a sentence, unless its worded "The best solution is NOT to use a cursor"

There are plenty of sql articles on recursion in sql. If performance isn't mission critical and you want to keep it simple (depending on if its N levels deep of recursion) using a table type and a while loop (articles all over on this implementation) is still better than a cursor.



________________________________________________
The only cure for thinking is picking rocks, or drinking beer.
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2006-01-09 : 12:53:14
Is the maximum number of levels finite, and modest (preferably 5 or less, certainly less than 10)?

if so I would just hardcode from LEFT OUTER JOINs to 5 (or 10!!) levels of recursion.

Kristen
Go to Top of Page

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2006-01-09 : 15:48:03
Those guys gave you all the info you need, but I like doing these things so...
use pubs
set nocount on
go

create table junk
(CategoryID int
,ParentCategoryID int
,Category varchar(30))
go

insert junk
select 65, NULL, 'Economic Development' union all
select 66, 65, 'A Wealth of Opportunity' union all
select 67, NULL, 'Top Category' union all
select 69, 67, 'test 3' union all
select 73, NULL, 'A new category' union all
select 74, 73, 'test' union all
select 75, 73, 'a test' union all
select 80, 75, 'a 3rd level test' union all
select 81, 80, 'a 4th level test'

go

declare @tb table
(CategoryID int
,hierarchy varchar(7900)
,category varchar(30)
,lev int)

declare @level int
set @level = 0

insert @tb (CategoryID, hierarchy, category, lev)
select categoryid
,category
,category
,@level
from junk
where parentCategoryid is null

--loop through an entire nesting level at once
while @@rowcount > 0
begin
set @level = @level + 1

insert @tb (CategoryID, hierarchy, category, lev)
select a.categoryid
,b.hierarchy + '.' + a.category
,a.category
,@level
from junk a
join @tb b
on b.CategoryID = a.parentCategoryid

--prevent circular references
left join @tb ex
on ex.CategoryID = a.Categoryid
where ex.CategoryID is null

and b.lev+1 = @level

end

--return results in hierarchal, alpabetical order
select category
,space(lev*3) + category [visual hierarchy]
from @tb
order by hierarchy

go
drop table junk


Be One with the Optimizer
TG
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-01-10 : 01:17:48
Also refer
http://www.nigelrivett.net/RetrieveTreeHierarchy.html

Madhivanan

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

Rogz
Starting Member

2 Posts

Posted - 2006-01-10 : 12:32:50
hey guys...thanks for the replies...I was under a time cruch so this is what i ended up doing...

-created a temp table to hold the parent categories
-created a temp table to hold the parent categories and children
-selected the parent categories into the temp table
-loop through the category temp table, insert the parent category into the second temp table, select the children for the current category and insert them into the second temp table
-select all rows from the second temp table to return

Parent categories will only have one level of child categories which helped to keep things simple and the amount of cateogries is small. i realize that this solution probably isn't the best way to do it....any thoughts for improvement?

thanks
Go to Top of Page

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2006-01-10 : 13:01:14
>>Parent categories will only have one level of child
>>any thoughts for improvement?

If you really only have 1 level deep children then don't do any looping, use Kristen's method of a single left outer join.

Otherwise, did you check out the code I posted? It only uses a single table and allows for unlimited nesting as well as returns the results in the order you specified initially. (you can run the code block as is to test)

Be One with the Optimizer
TG
Go to Top of Page
   

- Advertisement -