Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parent reference structure

Status
Not open for further replies.

DomTrix

Programmer
Dec 28, 2004
94
GB
I have a table of products that are all categorised. Each product has a categoryID which is a foreign key linked with the Primary Key in the Category table (category.cat_ID).

Each category can also have a parent Category so that a tree like structure is created. The levels deep for this tree is unlimited.

I want to be able to check whether a given product is beneath a given category in the tree, i.e.

Code:
category tree:

->all
    ->Books
        ->theory
            ->poetic
    ->CDs
    ->DVDs

If the product was a book on poetic theory, it would be beneath the category if the given category was books, theory or poetic; and not if the category was CDs or DVDs.

My initial thought was to simply create a table variable that gets passed to my procedure as an inout parameter. The process then populates this table with the given categoryID and then all of it's children, and all of their children, etc.

The calling procedure can then use this table to check if the products category ID is present or not.

This is all well and good i think and I could write the code in ColdFusion or C++, but coming to do it in SQL is proving hard on my brain!

I want the procedure to accept 2 things:

@table - the table object that is being populated (inout)
@parentID - the ID of the current category that we are looking for children for (in).

It then must select into the @table all categories that have @parentID as their parent AND for each of these I must call the procedure again (internally looping it), giving the current category as the @parentID.

Does this make sense? And if so, is it possible with SQL code?

 
> The levels deep for this tree is unlimited

Then classic parent-child model is very likely not good enough.

> Does this make sense? And if so, is it possible with SQL code?

With or without looping of any kind (temp table, cursors...)?

Anyway, you may find faq183-5322 useful.

------
"There's a man... He's bald and wears a short-sleeved shirt, and somehow he's very important to me. I think his name is Homer."
(Jack O'Neill, Stargate)
[banghead]
 
Great, thanks for the link, hav found my solution :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top