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
 imitating nested for each loop in SQL Query

Author  Topic 

SqlNewBee123
Starting Member

1 Post

Posted - 2007-05-25 : 16:14:37
Dear All,

I need to create a query to list all the subfolders within a folder.

I have a database table that lists the usual properties of each of the folder.

I have another database table that has two columns

1. Parent folder
2. Child folder

But this table maintains the parent child relationship only to one level.

For example if i have a folder X that has a subfolder Y and Z.
And Y has subfolders A and B.
and B has subfolder C and D
and C has subfolder E and F

The database table will look like

parentfolder child folder
X Y
X Z
Y A
Y B
B C
B D
C E
C F

I want to write a query which will take a folder name as the input and will provide me a list of all the folders and subfolders under it. The query should be based on the table (parent - child) and there should not be any restriction on the subfolder levels to search and report for.

I have been banging my head to do this but i have failed so far. Any help on this will be highly appreciated.

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2007-05-25 : 16:20:07
if you're using SQL SErver 2005 you can use CTE's = common table expression for this.
google has many examples.

if not then read this article:
http://www.sqlteam.com/item.asp?ItemID=8866

_______________________________________________
Causing trouble since 1980
blog: http://weblogs.sqlteam.com/mladenp
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-05-26 : 01:44:58
Also refer
http://www.nigelrivett.net/RetrieveTreeHierarchy.html


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

jezemine
Master Smack Fu Yak Hacker

2886 Posts

Posted - 2007-05-26 : 03:10:10
also Celko's nested set model is worth a look: http://www.google.com/search?q=nested+set+celko

EDIT: oops, just noticed this was already mentioned in the article Mladen linked to.


www.elsasoft.org
Go to Top of Page
   

- Advertisement -