Oracle層級詢語句connect by 用法詳解
阿新 • • 發佈:2019-02-14
SQL> select e.empno, e.ename, e.mgr, level, connect_by_iscycle "CYCLE"
2 from emp e
3 connect by nocycle prior e.empno = e.mgr
4 start with e.ename = 'KING';
EMPNO ENAME MGR LEVEL CYCLE
----- ---------- ----- ---------- ----------
7839 KING 7788 1 0
7566 JONES 7839 2 0
7788 SCOTT 7566 3 1
7876 ADAMS 7788 4 0
7902 FORD 7566 3 0
7369 SMITH 7902 4 0
7698 BLAKE 7839 2 0
7499 ALLEN 7698 3 0
7521 WARD 7698 3 0
7654 MARTIN 7698 3 0
7844 TURNER 7698 3 0
7900 JAMES 7698 3 0
7782 CLARK 7839 2 0
7934 MILLER 7782 3 0
2 from emp e
3 connect by nocycle prior e.empno = e.mgr
4 start with e.ename = 'KING';
EMPNO ENAME MGR LEVEL CYCLE
----- ---------- ----- ---------- ----------
7839 KING 7788 1 0
7566 JONES 7839 2 0
7788 SCOTT 7566 3 1
7876 ADAMS 7788 4 0
7902 FORD 7566 3 0
7369 SMITH 7902 4 0
7698 BLAKE 7839 2 0
7499 ALLEN 7698 3 0
7521 WARD 7698 3 0
7654 MARTIN 7698 3 0
7844 TURNER 7698 3 0
7900 JAMES 7698 3 0
7782 CLARK 7839 2 0
7934 MILLER 7782 3 0