Reflexive treeview table - how to get root from child?

  • Hi All,

    I  have a table used for a treeview control. The table looks like this:

    CategoryIDParentCategoryIDCategoryName
    1-1parent
    21child
    32child of child

    Given a CategoryID how could I work all the way back up the tree to get the top or root level CategoryID?

    In this example given a CategoryID of 3 I would need to work all the way back up to arrive a the top level and return a CategoryID of 1 (ParentCategoryID of -1 signifies top level)

    Since this is a tree view there could be many nodes so some sort of loop/recursive function would be needed I guess until I get to -1, but despite my research I just don't know where to start...

    Any help greatly appreciated!!

    Sal

  • There are a lot of ways to do that but one of my favorites is this one

    HTH

     


    * Noel

  • In my opinion, adding two another columns is a better option,but it will need extra maintenance efforts. If the tree structure is changed frequently, keeping the table simple is better.

    You can create UDF to get the whole structure based on the specified root ID. That's easy and much more flexible.

  • blueagle,
     
    You posted...
    >You can create UDF to get the whole structure based on the specified root ID. That's easy and much more flexible. 
     
    Mind telling us how to do that?  
     
    "It's easy to get to the moon... first you build a rocket ship then..."

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply