CONNECT_BY_ROOT
The CONNECT_BY_ROOT unary operator is for use only in hierarchical queries. For every row in the hierarchy, this operator returns the expression for the row's root ancestor.
- expression
- An expression that does not contain a NEXT VALUE expression, a hierarchical query construct (such as the LEVEL pseudocolumn), the SYS_CONNECT_BY_PATH function, or an OLAP specification.
The result data type and length of the operator is the same as the result data type and length of the expression.
A CONNECT_BY_ROOT operator cannot be specified in the START WITH clause or the CONNECT BY clause of a hierarchical query. It cannot be specified as an argument to the SYS_CONNECT_BY_PATH function.
A CONNECT_BY_ROOT operator has a higher precedence than
any infix operator. Therefore, to pass an expression with infix operators
(such as + or ||) as an argument, parentheses must be used. For example:
CONNECT_BY_ROOT FIRSTNME || LASTNAME
returns
the FIRSTNME value of the root ancestor row concatenated with the
LASTNAME value of the actual row in the hierarchy, because this expression
is equivalent to:(CONNECT_BY_ROOT FIRSTNME) || LASTNAME
rather
than:CONNECT_BY_ROOT (FIRSTNME || LASTNAME)
Example
- Return the hierarchy of departments and their root departments
in the DEPARTMENT table.
This query returns:SELECT CONNECT_BY_ROOT DEPTNAME AS ROOT, DEPTNAME FROM DEPARTMENT START WITH DEPTNO IN ('B01','C01','D01','E01') CONNECT BY PRIOR DEPTNO = ADMRDEPT
ROOT DEPTNAME ------------------ ----------------------- PLANNING PLANNING INFORMATION CENTER INFORMATION CENTER DEVELOPMENT CENTER DEVELOPMENT CENTER DEVELOPMENT CENTER MANUFACTURING SYSTEMS DEVELOPMENT CENTER ADMINISTRATION SYSTEMS SUPPORT SERVICES SUPPORT SERVICES SUPPORT SERVICES OPERATIONS SUPPORT SERVICES SOFTWARE SUPPORT SUPPORT SERVICES BRANCH OFFICE F2 SUPPORT SERVICES BRANCH OFFICE G2 SUPPORT SERVICES BRANCH OFFICE H2 SUPPORT SERVICES BRANCH OFFICE I2 SUPPORT SERVICES BRANCH OFFICE J2