affiliate marketing Interview Qns: Can I implement Tree Structured Queries ?

Monday, October 25, 2010

Can I implement Tree Structured Queries ?

Yes! This is commonly asked by those migrating from non-RDBMS apps. This is definitely non-relational (enough to kill Codd and then make him roll in his grave) and is a feature I have not seen in the competition.
The definitive example is in the example SCOTT/TIGER database, when looking at the EMP table (EMPNO and MGR columns). The MGR column contains the employee number of the "current" employee's boss.
You have available an extra pseudo-column, LEVEL, that says how deep in the tree you are. Oracle can handle queries with a depth up to 255.
select LEVEL, EMPNO, ENAME, MGR
from EMP
connect by prior EMPNO = MGR
start with MGR is NULL;
You can get an "indented" report by using the level number to substring or lpad a series of spaces and concatenate that to the string.
select lpad(' ', LEVEL * 2) || ENAME ........
You use the start with clause to specify the start of the tree(s). More than one record can match the starting condition.
One disadvantage of a "connect by prior" is that you cannot perform a join to other tables. Still, I have not managed to see anything else like the "connect by prior" in the other vendor offerings and I like trees. Even trying to doing this programmatically in embedded SQL is difficult as you have to do the top level query, for each of them open a cursor to look for lower level rows, for each of these.......
soon you blow the cursor limit for your installation.
The way around this is to use PL/SQL, open the driving cursor with the "connect by prior" statement, and the select matching records from other tables on a row-by-row basis, inserting the results into a temporary table for later retrieval.
Note that you can't trick Oracle by using CONNECT BY PRIOR on a view that does the join.

No comments:

Post a Comment