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.
| Author |
Topic |
|
vladibo
Starting Member
11 Posts |
Posted - 2004-06-07 : 10:26:36
|
| Hello,I have table that links recursively in order to create hierarchical structure like this:CREATE TABLE category(category_id int,category varcher(255),parent_id int);With the help of this beautiful forum I have found how to get all the parent IDs for a specific record. Its like this:CREATE PROCEDURE pr_GetAllParentIds@category_id intASDeclare @parent_id intDeclare @temp table(cat_id int)SELECT @parent_id=parent_id FROM category WHERE category_id=@category_idIF @@ROWCOUNT>0BEGIN INSERT INTO @temp VALUES (@parent_id) SELECT @parent_id=parent_id FROM category WHERE category_id=@category_idENDSELECT * FROM @tempGOBut I don't know how to get all the child records for a specific categoryThanks in advance |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2004-06-07 : 11:07:59
|
| insert @temp select @category_idwhile @@ROWCOUNT>0BEGININSERT @tempSELECT distinct c.parent_id FROM category cleft join @temp ton c.parent_id = t.cat_idWHERE t.cat_id is nullEND==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
|
|
|