Friday, October 23, 2009

Recursive SQL statement for Oracle database

Many people may still in doubt about how to do query against table which link recursively to itself. i.e. if you want to select all tree relate to folder 'A' from data as shown in the sample table below
Folders
 +-----------+-------------+
| Folder | ParentFolder|
| --------- | ----------- |
| A | B |
| B | C |
| E | A |


This can be done by using start with ... connect by clause like below

select folder from folders start with groupname = 'A'
connect by prior parentfolder = folder



Enjoy coding!


0 comments:

Post a Comment