CREATE TABLE emp (eid NUMBER(3), ename VARCHAR2(20), mgr NUMBER(3));
BEGIN
INSERT INTO emp VALUES(100 , 'Murugappan', NULL);
INSERT INTO emp VALUES(101 , 'Senthil' , 100);
INSERT INTO emp VALUES(102 , 'Karthic' , 101);
INSERT INTO emp VALUES(103 , 'John' , 102);
INSERT INTO emp VALUES(104 , 'Nagaraj' , 100);
INSERT INTO emp VALUES(105 , 'Selva' , 104);
END;
/
Sample 1:
SELECT eid, ename, mgr, PRIOR ename, LEVEl
FROM emp
CONNECT BY PRIOR eid = mgr
START WITH mgr IS NULL;
Sample Output
Sample 2:
SELECT lpad(' ',5*level-1)||ename as ename
FROM emp
START WITH mgr IS NULL
CONNECT BY NOCYCLE PRIOR eid = mgr;
Sample output
-----------------------
ENAME
-----------------------
Murugappan
Senthil
Karthic
John
Nagaraj
Selva
-----------------------
Sample 3:
SELECT eid, LPAD(' ', LEVEL*5, ' ')||ename NAME , mgr, PRIOR ename, LEVEL
FROM emp
CONNECT BY PRIOR eid = mgr
START WITH mgr IS NULL;
Sample output
DROP TABLE emp;
No comments:
Post a Comment