November 21, 2003 at 8:37 am
Hello gurus!
I'm trying to arrange some articles (links to articles that is) into a category structure. Now, I think I know the basics of storing hierarchial data in a table. It should be something like this:
myTable
categoryID
categoryName
parentID
Now, my question is, how do I select the categories?
I know I can get the main categories like this:
SELECT * FROM myTable WHERE parentID = NULL
or
SELECT * FROM myTable WHERE parentID = categoryID
And then I can get the subcategories or a specific category like so:
SELECT * FROM myTable WHERE parentID = @parentID
But how do I get all the categories nicely arranged in a single query?
I would like the output to be something like this:
- biology
-- prehistoric
--- dinosaurs
--- mammals
-- monkeys
--- baboons
....
Well, you get the picture. Is there a simple, easy and fast solution to do this?
/Tomi
PS. This is my 100th post! Whoooopie!
Edited by - tomiz on 11/21/2003 08:40:23 AM
November 21, 2003 at 8:56 am
Not unless you can accept a depth limit.
For infinite-depth heirarchies I believe you are stuck with doing it yourself in a loop.
Look in online help for 'expanding hierarchies'
November 21, 2003 at 11:53 am
Well, that's exactly what I'm looking for! I guess I can have a limit...
Thanks a lot!
/Tomi
November 21, 2003 at 11:56 am
I meant that the other way- if you custom code a loop to build a heirarchy from a rowset of child/sponsor records, you don't have to worry about a depth limit like you do when you use joins (number of involved tables limit) or UDFs (nesting limit).
November 21, 2003 at 12:20 pm
Oh, I see... Thanks again!
/Tomi
November 24, 2003 at 12:35 am
quote:
But how do I get all the categories nicely arranged in a single query?I would like the output to be something like this:
- biology
-- prehistoric
--- dinosaurs
--- mammals
-- monkeys
--- baboons
....
Well, you get the picture. Is there a simple, easy and fast solution to do this?
/Tomi
PS. This is my 100th post! Whoooopie!
Congrats!
Most easy one would be to do this in your client aplication.
Maybe you'll get some ideas here:
http://support.microsoft.com:80/support/kb/articles/q186/1/33.asp&NoWebContent=1
I don't know if it is availabel yet, but Joe Celko has a book on Tree and Hierarchies in SQL.
Frank
--
Frank Kalis
Microsoft SQL Server MVP
Webmaster: http://www.insidesql.org/blogs
My blog: http://www.insidesql.org/blogs/frankkalis/[/url]
November 24, 2003 at 1:16 am
quote:
Maybe you'll get some ideas here:http://support.microsoft.com:80/support/kb/articles/q186/1/33.asp&NoWebContent=1
Great article! Thanks Frank!
/Tomi
November 24, 2003 at 4:38 am
This one might also be of interest to you
http://vyaskn.tripod.com/hierarchies_in_sql_server_databases.htm
Frank
--
Frank Kalis
Microsoft SQL Server MVP
Webmaster: http://www.insidesql.org/blogs
My blog: http://www.insidesql.org/blogs/frankkalis/[/url]
November 24, 2003 at 7:23 am
Hehe! Already read that one
May 20, 2009 at 1:28 am
Thanks for the 'Expanding Hierarchies'
Been struggling for 3 days and clearly been googling the wrong terminology.
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply