Sys_connect_by_path is a Oracle feature (Introduced in Version 9i) which is used in hierarchical queries to return the position of a child element in a hierarchical structure
vis-a-vis its parent.
For example if one wants to know the position of
an employee with respect to the owner of the company
in a tree-like structure, the sys_connect_by_path
feature makes the task really simple.
Usage :-
SELECT SYS_CONNECT_BY_PATH( < column > ,
< seperator > )
FROM ...
CONNECT BY ...
Here, < column > denotes the table column name
whose path position we need to see, and < seperator >
denotes the character which SYS_CONNECT_BY_PATH
uses to seperate the each position value from the
other in the hierarchical tree structure.
Let us see an Example :-
Here, we have a Table "parentchild", which shows information about each Child and its parent as below.
select * from parentchild order by child
--------------------
CHILD PARENT
--------- ---------
3
30 3
31 3
32 3
33 3
300 30
301 30
302 30
312 31
313 31
--------------------
Now using the Oracle feature sys_connect_by_path,
let us see how each child element figures in the hierarchical structure :-
select parent, child, sys_connect_by_path(child,'/') path ,level
from parentchild
connect by parent = prior child
order by child
----------------------------------------
PARENT CHILD PATH LEVEL
--------- -------- ------------- -----
3 /3 1
3 30 /3/30 2
3 30 /30 1
3 31 /31 1
3 31 /3/31 2
3 32 /3/32 2
3 32 /32 1
3 33 /3/33 2
3 33 /33 1
30 300 /300 1
30 300 /30/300 2
30 300 /3/30/300 3
30 301 /3/30/301 3
30 301 /30/301 2
30 301 /301 1
30 302 /30/302 2
30 302 /302 1
30 302 /3/30/302 3
31 312 /3/31/312 3
31 312 /312 1
31 312 /31/312 2
31 313 /31/313 2
31 313 /3/31/313 3
31 313 /313 1
----------------------------------------
In this example sys_connect_by_path uses the character '/'
to seperate the elements in the hierarchy and displays the 'Child'
column value in the hierarchy.
The sys_connect_by_path could be used only in a Oracle hierarchical query in conjunction with the connect by clause (which defines the child parent path).