| |
 |
|
Oracle SQL WITH clause
connect by isleaf
Oracle Tips by Burleson Consulting
May 20, 2010
|
Also see my
Oracle recursive WITH clause notes.
Oracle connect by
isleaf
Oracle author
Laurent Schneider has this great explanation of the Oracle
connect by isleaf syntax:
The 10g query using
connect_by_isleaf:
with o as
(
SELECT 'A' obj, 'B' link from dual union all
SELECT 'A', 'C' from dual union all
SELECT 'C', 'D' from dual union all
SELECT
'D', 'C' from dual union all
SELECT
'D', 'E' from dual union all
SELECT 'E', 'E' from dual)
select connect_by_root obj root,level,obj,link,
sys_connect_by_path(obj||
'->'
||link,','),
connect_by_iscycle,
connect_by_isleaf
from o
connect by nocycle obj=prior link
start with obj='A';
ROOT LEVEL O L PATH
CYCLE LEAF
---- ----- - - -------------------- ----- -----
A 1 A B ,A->B 0
1
A 1 A C ,A->C 0
0
A 2 C D ,A->C,C->D
1 0
A 3 D E ,A->C,C->D,D->E 1
1
Obviously in 10g the
connect by nocycle does works well with that kind of graphs, D-C and
E-E are missing and C-D and D-E are marked as cycling…
Let’s try the 11gR2
equivalency.
with o(obj,link) as
(
SELECT 'A', 'B' from dual union all
SELECT 'A', 'C' from dual union all
SELECT 'C', 'D' from dual union all
SELECT
'D', 'C' from dual union all
SELECT
'D', 'E' from dual union all
SELECT 'E', 'E' from dual),
t(root,lev,obj,link,path) as (
select obj,1,obj,link,cast(obj||'->'||link
as varchar2(4000))
from o
where obj='A' -- START WITH
union all
select
t.root,t.lev+1,o.obj,o.link,
t.path||', '||o.obj||
'->'
||o.link
from t, o
where t.link=o.obj
)
search depth first by obj set ord
cycle obj set cycle to 1 default 0
select root,lev,obj,link,path,cycle,
case
when (lev - lead(lev) over (order by ord)) < 0
then 0
else 1
end is_leaf
from
t;
ROOT LEV OBJ LINK PATH CYCLE IS_LEAF
---- ---- ---- ---- --------------------------- ----- -------
A 1 A B A->B 0
1
A 1 A C A->C 0
0
A 2 C D A->C, C->D 0
0
A 3 D C A->C, C->D, D->C 0
0
A 4 C D A->C, C->D, D->C, C->D 1
1
A 3 D F A->C, C->D, D->E 0
0
A 4 F F A->C, C->D, D->E, E->E 0
0
A 5 F F A->C, C->D, D->E, E->E, E->E 1
1
|
|
|
|
Guarantee your Success!
Oracle is the
world's most complex, robust and flexible database, considered
impossible to master without a mentor.
That's why all BC
Oracle trainers are working professionals, experts in Oracle who
share their tips and secrets. |
|
| |
|
Burleson is the American Team

Note:
This Oracle
documentation was created as a support and Oracle training reference for use by our
DBA performance tuning consulting professionals.
Feel free to ask questions on our
Oracle forum.
Verify
experience!
Anyone
considering using the services of an Oracle support expert should
independently investigate their credentials and experience, and not rely on
advertisements and self-proclaimed expertise. All legitimate Oracle experts
publish
their Oracle
qualifications.
Errata?
Oracle technology is changing and we
strive to update our BC Oracle support information. If you find an error
or have a suggestion for improving our content, we would appreciate your
feedback. Just
e-mail:
and include the URL for the page.
Copyright ? 1996 - 2011 by Burleson Enterprises
All rights reserved.
Oracle ?
is the registered trademark of Oracle Corporation.
|
|
|
|
|