To evaluates hierarchical query oracle provides PRIOR expression of CONNECT BY condition.
Example
CREATE TABLE employees
(
empid INT PRIMARY KEY,
ename VARCHAR2(30) NOT NULL,
designation VARCHAR2(20),
parent_id INT
);
SELECT * FROM EMPLOYEES;
EMPID ENAME DESIGNATION PARENT_ID
---------- ------------------------------ -------------------- ----------
1 P.P.TYAG VALLABH SWAMIJI SECRETARY
2 Dr.J.N.Shah DIRECTOR 1
3 Dr.G.D.Acharya PRINCIPAL 2
4 Dr.S.R.VARMA PRINCIPAL 2
5 Dr.K.R.VADALIYA PRINCIPAL 2
6 Dr.G.C.JOSHI PRINCIPAL 2
7 Dr.A.U.PATEL PRINCIPAL 2
8 Dr.NISHANT VACHHANI HOD 4
9 Dr.ASHISH KOTHARI HOD 3
10 PROF.J.N.RATHOD HOD 3
11 PROF.P.C.SHUKLA HOD 4
12 DR.STAVAN PATEL HOD 7
13 PROF.ANKIT FALDU ASSISTANT PROFESSOR 11
14 DR.PRATIK VANZARA ASSISTANT PROFESSOR 12
15 PROF.VIVEK VYAS ASSISTANT PROFESSOR 11
16 PROF.KALPESH POPAT ASSISTANT PROFESSOR 11
17 PROF.PRAKASH GUJARATI ASSISTANT PROFESSOR 12
18 PROF.DIVYESH GOHEL ASSISTANT PROFESSOR 12
19 DR.AMIT RAJDEV ASSISTANT PROFESSOR 8
20 PROF.ANKIT GANDHI ASSISTANT PROFESSOR 8
20 rows selected.
SELECT Sys_connect_by_path(ename, '-') "Path"
FROM employees
START WITH Upper(ename) = 'P.P.TYAG VALLABH SWAMIJI'
CONNECT BY PRIOR empid = parent_id
SELECT ename, designation, LEVEL
FROM employees
START WITH Upper(ename) = 'P.P.TYAG VALLABH SWAMIJI'
CONNECT BY PRIOR empid = parent_id
Example
CREATE TABLE employees
(
empid INT PRIMARY KEY,
ename VARCHAR2(30) NOT NULL,
designation VARCHAR2(20),
parent_id INT
);
SELECT * FROM EMPLOYEES;
EMPID ENAME DESIGNATION PARENT_ID
---------- ------------------------------ -------------------- ----------
1 P.P.TYAG VALLABH SWAMIJI SECRETARY
2 Dr.J.N.Shah DIRECTOR 1
3 Dr.G.D.Acharya PRINCIPAL 2
4 Dr.S.R.VARMA PRINCIPAL 2
5 Dr.K.R.VADALIYA PRINCIPAL 2
6 Dr.G.C.JOSHI PRINCIPAL 2
7 Dr.A.U.PATEL PRINCIPAL 2
8 Dr.NISHANT VACHHANI HOD 4
9 Dr.ASHISH KOTHARI HOD 3
10 PROF.J.N.RATHOD HOD 3
11 PROF.P.C.SHUKLA HOD 4
12 DR.STAVAN PATEL HOD 7
13 PROF.ANKIT FALDU ASSISTANT PROFESSOR 11
14 DR.PRATIK VANZARA ASSISTANT PROFESSOR 12
15 PROF.VIVEK VYAS ASSISTANT PROFESSOR 11
16 PROF.KALPESH POPAT ASSISTANT PROFESSOR 11
17 PROF.PRAKASH GUJARATI ASSISTANT PROFESSOR 12
18 PROF.DIVYESH GOHEL ASSISTANT PROFESSOR 12
19 DR.AMIT RAJDEV ASSISTANT PROFESSOR 8
20 PROF.ANKIT GANDHI ASSISTANT PROFESSOR 8
20 rows selected.
SELECT Sys_connect_by_path(ename, '-') "Path"
FROM employees
START WITH Upper(ename) = 'P.P.TYAG VALLABH SWAMIJI'
CONNECT BY PRIOR empid = parent_id
SELECT ename, designation, LEVEL
FROM employees
START WITH Upper(ename) = 'P.P.TYAG VALLABH SWAMIJI'
CONNECT BY PRIOR empid = parent_id
0 comments:
Post a Comment