Selecting hierarchial data

  • 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

  • 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'

  • Well, that's exactly what I'm looking for! I guess I can have a limit...

    Thanks a lot!

    /Tomi

  • 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).

  • Oh, I see... Thanks again!

    /Tomi

  • 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

    http://www.insidesql.de

    http://www.familienzirkus.de

    --
    Frank Kalis
    Microsoft SQL Server MVP
    Webmaster: http://www.insidesql.org/blogs
    My blog: http://www.insidesql.org/blogs/frankkalis/[/url]

  • 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

  • This one might also be of interest to you

    http://vyaskn.tripod.com/hierarchies_in_sql_server_databases.htm

    Frank

    http://www.insidesql.de

    http://www.familienzirkus.de

    --
    Frank Kalis
    Microsoft SQL Server MVP
    Webmaster: http://www.insidesql.org/blogs
    My blog: http://www.insidesql.org/blogs/frankkalis/[/url]

  • Hehe! Already read that one

  • 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