Using Oracle 10g. I have two tables:
User Parent
-------------
1 (null)
2 1
3 1
4 3
Permission User_ID
-------------------
A 1
B 3
The values in the permissions table get inherited down to the children. I would like to write a single query that could return me something like this:
User Permission
------------------
1 A
2 A
3 A
3 A
3 B
4 A
4 B
Is it possible to formulate such a query using 10g connect .. by syntax to pull in rows from previous levels?
Kind of black magic, but you can use table-cast-multiset to reference one table from another in WHERE clause:
In the subquery
x
I construct a table of all parents for the given user fromt1
(including itself), then join it with permissions for these parents.Here is a example for just one user id. you can use proc to loop all.
You could take a look at http://www.adp-gmbh.ch/ora/sql/connect_by.html
you can achieve the desired result with a connect by (and the function
CONNECT_BY_ROOT
that returns the column value of the root node):