0

I am trying to implement hierarchical query in my project. I have read most of the documents on internet but unable to get information related to use of PRIOR in it.

SELECT EMPNO,ENAME,MGR,LEVEL
FROM TMP_PCH
CONNECT BY PRIOR EMPNO=MGR
START WITH MGR IS NULL;

PRIOR plays significant role in this query but changing the position of PRIOR or Removing it from query gives me a different result.
It would be really helpful if you can help me to understand exact use of this.
Thanks :)

2 Answers 2

2

Hierarchy means, that one row of a table is a "parent", and another one - a "child". PRIOR is used to show who is who. Clause CONNECT BY PRIOR EMPNO = MGR means, that if two rows have the same value, but one row in a column EMPNO, and the second - in a column MGR, then second is a "parent" and first is a "child". So, query

SELECT EMPNO,ENAME,MGR,LEVEL
FROM TMP_PCH
CONNECT BY PRIOR EMPNO = MGR
START WITH MGR = 'John'

returns all subordinates of John (and John itself), and query

SELECT EMPNO,ENAME,MGR,LEVEL
FROM TMP_PCH
CONNECT BY PRIOR MGR = EMPNO
START WITH MGR = 'John'

returns all bosses of John (and John itself).

0
1

you can understand PRIOR as a reference to the previous hierarchy level.
PRIOR MGR = EMPNO means "the manager from the previous level is now the employee", so we are going bottom-up
PRIOR EMPNO = MGR means "the employee from the previous level is now the manager", so we are going top-down

when you write it like this EMPNO = PRIOR MGR, you can read it as "we go the direction from an employee to his manager"

bottom-up

             MGR
PRIOR MGR    EMPNO
PRIOR EMPNO

vs top-down

PRIOR MGR
PRIOR EMPNO  MGR
             EMPNO
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.