Petre Popescu
Petre Popescu

Reputation: 2002

File tree in OracleSQL

I have a basic file system tree that consists of two tables:

folders
     =======================
     id_f    | (PK)
     name    |
     parent  | (FK references id_f)

and

files
     =======================
     name    |
     content | (File content / text)
     parent  | (FK references id_f)

I need to make a query that will display the filesystem as a tree view like this:

ROOT
   DIR1
      file1
      file2
   DIR2
   DIR3
      file
   DIR4
      DIR5
      DIR6

Any help on how to achive this output? Thanks

Upvotes: 1

Views: 185

Answers (1)

Dave Costa
Dave Costa

Reputation: 48121

I'd try this. Can't test it myself at the moment.

SELECT LPAD(' ', (level-1)*2) || name
FROM (
  SELECT id_f, name, parent FROM folders
  UNION ALL
  SELECT NULL, name, parent FROM files
  )
START WITH parent IS NULL
CONNECT BY parent = PRIOR id_f

Upvotes: 4

Related Questions