1. 程式人生 > >Oracle函式sys_connect_by_path()

Oracle函式sys_connect_by_path()

這個函式是oracle9i新提出來的,用來顯示分層查詢的路徑,從跟節點到子節點的路徑。

引數:sys_connect_by_path(欄位名, 2個欄位之間的連線符號)

注意:sys_connect_by_path()函式必須和connect by 關鍵字一起使用。

具體路徑怎麼顯示,來看一個例子:


--create table
create table a (id varchar2(10),name varchar2(20));
create table b (id1 varchar2(10),id2 varchar2(10));
--insert
insert into a
select 1,'a' from dual union all
select 2,'b' from dual union all
select 3,'c' from dual union all
select 4,'d' from dual union all
select 5,'e' from dual ;
insert into b
select null,1 from dual union all
select 1,2 from dual union all
select 2,3 from dual union all
select 2,4 from dual union all
select 3,5 from dual ;
commit;

select  * from a;
select  * from b;


--查詢
select  b.*,a.name,sys_connect_by_path(a.name,'/') path
from b right join a on b.id2 = a.id
start with b.id1 is null
connect by b.id1= prior b.id2
order by 2,1

結果: 

ID1 ID2 NAME PATH
1  1 a /a
2 1 2 b /a/b
3 2 3 c /a/b/c
4 2 4 d /a/b/d
5 3 5 e /a/b/c/e