Oracle CONNECT BY 用法
CONNECT BY PRIOR 是标示父子关系的对应
SELECT last_name, employee_id, manager_id, LEVEL
FROM employees
START WITH employee_id = 100
CONNECT BY PRIOR employee_id = manager_id
ORDER SIBLINGS BY last_name;
SELECT LPAD(' ', 5 * LEVEL, ' ') || ename empName,
dname,
job,
sys_connect_by_path(ename, '/') cbp
FROM scott.emp e, scott.dept d
WHERE e.deptno = d.deptno
START WITH mgr IS NULL
CONNECT BY PRIOR empno = mgr
ORDER SIBLINGS BY job;
EMPNAME DNAME JOB CBP
---------------- -------------- --------- ---------------------------
KING ACCOUNTING PRESIDENT /KING
JONES RESEARCH MANAGER /KING/JONES
SCOTT RESEARCH ANALYST /KING/JONES/SCOTT
ADAMS RESEARCH CLERK /KING/JONES/SCOTT/ADAMS
FORD RESEARCH ANALYST /KING/JONES/FORD
SMITH RESEARCH CLERK /KING/JONES/FORD/SMITH
CLARK ACCOUNTING MANAGER /KING/CLARK
MILLER ACCOUNTING CLERK /KING/CLARK/MILLER
BLAKE SALES MANAGER /KING/BLAKE
JAMES SALES CLERK /KING/BLAKE/JAMES
ALLEN SALES SALESMAN /KING/BLAKE/ALLEN
WARD SALES SALESMAN /KING/BLAKE/WARD
TURNER SALES SALESMAN /KING/BLAKE/TURNER
MARTIN SALES SALESMAN /KING/BLAKE/MARTIN
自从Since Oracle 9i 开始,就可以通过 SYS_CONNECT_BY_PATH 函数实现将从父节点到当前行内容以“path”或者层次元素列表的形式显示出来。 如下例所示:
如HN项目,取SWJG_LJ
select vjg.swjg_dm,
vjg.sj_swjg_dm,
vjg.swjg_lj swjg_lj1,
'14100000000' || sys_connect_by_path(swjg_dm, '#') swjg_lj2,
sys_connect_by_path(swjg_dm, '#') swjg_lj3
from ssfxpgcl.dm_swjg vjg
start with vjg.swjg_dm = '14101000000'
connect by prior swjg_dm = sj_swjg_dm
posted on 2013-10-12 16:50 风影ChinaYao 阅读(327) 评论(0) 编辑 收藏 举报