Exhibit 2 Connect by Example

You might also like

You are on page 1of 1

Exhibit 2 CONNECT BY Example

8/13/15, 8:47 PM

Exhibit 2 CONNECT BY Example


Category: PL/SQL Translation Tool
Published Date

Oracle CONNECT BY Example:


SELECT ename, empno, mgr, LEVEL
FROM emp
START WITH mgr IS NULL
CONNECT BY PRIOR empno = mgr;

Postgres as translated by magicHat


-- magicHat translation v2.1 July 30, 2014
WITH RECURSIVE rCTE ( ename, empno, mgr, LEVEL, arrHierarchy, cycle)
AS (
SELECT
-- the anchor query
ename,
empno,
mgr,
1,
ARRAY[coalesce(empno,0)], -- init array for hierarchy
FALSE
-- return false for cycle (hang detection)
FROM EMP
WHERE mgr IS NULL
-- START WITH condition
UNION ALL
SELECT
-- the recursive query
tn.ename,
tn.empno,
tn.mgr,
tp.LEVEL + 1,
arrHierarchy || tn.empno,
-- append to preserve the hierarchy
tn.empno = ANY(arrHierarchy) -- test for loop possibility (cycle)
FROM rCTE tp, EMP tn
WHERE tp.empno = tn.mgr AND cycle = FALSE
) -- end recursive CTE
SELECT ename, empno, mgr, LEVEL
FROM rCTE
WHERE cycle = FALSE
ORDER BY arrHierarchy;

http://postgresmigrations.com/database-migration/stored-procedure-tool/exhibit-2-connect-by-example?tmpl=component&print=1&page=

Page 1 of 1

You might also like