DB2 Version 10.1 for Linux, UNIX, and Windows

Hierarchical queries

A hierarchical query is a form of recursive query that retrieves a hierarchy, such as a bill of materials, from relational data by using a CONNECT BY clause.

Enablement

You enable hierarchical query support by setting the DB2_COMPATIBILITY_VECTOR registry variable to hexadecimal value 0x08 (bit position 4), and then stop and restart the instance to have the new setting take effect.
db2set DB2_COMPATIBILITY_VECTOR=08
db2stop
db2start
To take full advantage of the DB2® compatibility features for Oracle applications, the recommended setting for the DB2_COMPATIBILITY_VECTOR is ORA, which sets all of the compatibility bits.

You can then use CONNECT BY syntax, including pseudocolumns, unary operators, and the SYS_CONNECT_BY_PATH scalar function.

A hierarchical query contains a CONNECT BY clause that defines the join conditions between parent and child elements. Connect-by recursion uses the same subquery for the seed (START WITH clause) and the recursive step (CONNECT BY clause). This combination provides a concise method of representing recursions such as bills-of-material, reports-to-chains, or email threads.

Connect-by recursion returns an error if a cycle occurs. A cycle occurs when a row produces itself, either directly or indirectly. By using the optional CONNECT BY NOCYCLE clause, you can direct the recursion to ignore the duplicated row, thus avoiding both the cycle and the error. Hierarchical queries or connect-by recursion differs from DB2 recursion. For more information about the differences, see Port CONNECT BY to DB2.

hierarchical-query-clause

A subselect that includes a hierarchical-query-clause is called a hierarchical query.

Read syntax diagramSkip visual syntax diagram
>>-●--+-----------------------+--●--| connect-by-clause |--●---><
      '-| start-with-clause |-'                                

start-with-clause

|--START WITH--search-condition---------------------------------|

connect-by-clause

|--CONNECT BY--+---------+--search-condition--------------------|
               '-NOCYCLE-'                     

start-with-clause
START WITH denotes the seed of the recursion. The start-with-clause specifies the intermediate result table H1 for the hierarchical query. Table H1 consists of those rows of R for which the search-condition is true. If you do not specify the start-with-clause, H1 is the entire intermediate result table R. The rules for the search-condition within the start-with-clause are the same as those within the WHERE clause.
connect-by-clause
CONNECT BY describes the recursive step. The connect-by-clause produces the intermediate result table Hn+1 from Hn by joining Hn with R, using the search condition. If you specify the NOCYCLE keyword, the repeated row is not included in the intermediate result table Hn+1. An error is not returned. The rules for the search-condition within the connect-by-clause are the same as those within the WHERE clause, except that OLAP specifications cannot be specified (SQLSTATE 42903).

After a first intermediate result table H1 is established, subsequent intermediate result tables H2, H3, and so forth are generated. The subsequently created intermediate result tables are generated by joining Hn with table R using the connect-by-clause as a join condition to produce Hn+1. R is the result of the FROM clause of the subselect and any join predicates in the WHERE clause. The process stops when Hn+1 yields an empty result table. The result table H of the hierarchical-query-clause is the result as if UNION ALL were applied for every intermediate result table..

You can use the unary operator PRIOR to distinguish column references to Hn, the previous recursive step or parent, from column references to R. Consider the following example:
   CONNECT BY MGRID = PRIOR EMPID
MGRID is resolved with R, and EMPID is resolved within the columns of the previous intermediate result table Hn.

Rules

Notes

Examples