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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Iterating up an id tree

Author  Topic 

planetoneautomation
Posting Yak Master

105 Posts

Posted - 2010-10-22 : 10:03:57
if ...

SELECT al_item_id, al_description, al_father_id FROM ALL_LISTS where al_item_id = 1007

... returns al_father_id of 1006 and al_description of "ABC"

then if the sql is run again as:

SELECT al_item_id, al_description, al_father_id FROM ALL_LISTS where al_item_id = 1006

... and this time returns al_item_id of 1005 and al_description of "DEF"

then if the sql is run again as:

SELECT al_item_id, al_description, al_father_id FROM ALL_LISTS where al_item_id = 1005

... and this time returns al_item_id of 2 and al_description of "GHI"

then I can construct a string:

"ABC" + "DEF" + "GHI" = "ABC\DEF\GHI"

Question: Is there one sql statement that can be constructed that will take the initial al_item_id of 1007 and iterate until the al_item_id = 2 and construct the string which is a concatenation of all the al_description values ("ABC\DEF\GHI") with "\" between them?

denis_the_thief
Aged Yak Warrior

596 Posts

Posted - 2010-10-22 : 10:40:17
This shall point you in the right direction:

[url]http://msdn.microsoft.com/en-us/library/ms186243.aspx[/url]
Go to Top of Page
   

- Advertisement -