PRIOR unary operator

The PRIOR unary operator is for use only in the CONNECT BY clause of hierarchical queries. To get all subordinates over all levels, the PRIOR operator must be added to the CONNECT BY clause of the hierarchical query. This capability does not apply to the Entry plan of the Db2® Warehouse on Cloud managed service.

Read syntax diagramSkip visual syntax diagramPRIORexpression
expression
Any expression that does not contain a NEXT VALUE expression, an hierarchical query construct (such as the LEVEL pseudocolumn), the SYS_CONNECT_BY_PATH function, or an OLAP function. If you specify any of these items, SQLSTATE 428H4 is returned.

Usage

The CONNECT BY clause performs an inner join between the intermediate result table Hn of a hierarchical query and the source result table that you specify in the FROM clause. All column references to tables that are referenced in the FROM clause and that are arguments to the PRIOR operator are considered to range over table Hn.

The result data type of the operator is the result data type of the expression.

As shown in the following example, you typically join the primary key of the intermediate result table Hn to the foreign keys of the source result table to recursively traverse the hierarchy:.
   CONNECT BY PRIOR T.PK = T.FK
If the primary key is a composite key, prefix each column with PRIOR, as shown in the following example:
   CONNECT BY PRIOR T.PK1 = T.FK1 AND PRIOR T.PK2 = T.FK2
A PRIOR operator has a higher precedence than any infix operator, such as the plus sign (+) or double vertical bar (||). Therefore, to pass an expression with infix operators as an argument, you must use parentheses. The parentheses surrounding groups of operands and operators are necessary to indicate the intended order in which operations are to be performed. For example, the following expression returns the FIRSTNME value of the prior row concatenated with the LASTNAME value of the actual row in the hierarchy:
   PRIOR FIRSTNME || LASTNAME
That expression is equivalent to the first one in the following list but not the second one:
   (PRIOR FIRSTNME) || LASTNAME
   PRIOR (FIRSTNME || LASTNAME)

If you specify the PRIOR operator outside a CONNECT BY clause of a hierarchical query, SQLSTATE 428H4 is returned.

Example

  • The following query returns the hierarchy of departments in the DEPARTMENT table:
       SELECT LEVEL, DEPTNAME
         FROM DEPARTMENT START WITH DEPTNO = 'A00'
         CONNECT BY NOCYCLE PRIOR DEPTNO = ADMRDEPT
    This query returns the following results:
    LEVEL       DEPTNAME
    ----------- ----------------------------------------
              1 SPIFFY COMPUTER SERVICE DIV.
              2 PLANNING
              2 INFORMATION CENTER
              2 DEVELOPMENT CENTER
              3 MANUFACTURING SYSTEMS
              3 ADMINISTRATION SYSTEMS
              2 SUPPORT SERVICES
              3 OPERATIONS
              3 SOFTWARE SUPPORT
              3 BRANCH OFFICE F2
              3 BRANCH OFFICE G2
              3 BRANCH OFFICE H2
              3 BRANCH OFFICE I2
              3 BRANCH OFFICE J2