Convert Oracle CONNECT BY to WITH RECURSIVE
CONNECT BY is Oracle's own way to walk a tree, such as an org chart. Other databases use a recursive WITH query instead. Dotty reads the Oracle query, runs it so you can check the rows, and writes it out for the database you are moving to.
1. Load the Oracle query
On the Dotty home screen, choose Load and pick a .sql file. This one lists everyone under the top manager, with how deep each person is and the chain of managers above them:
CREATE TABLE employees (employee_id INTEGER, name VARCHAR(30), manager_id INTEGER);
INSERT INTO employees VALUES
(1, 'Grace Lin', NULL),
(2, 'Omar Haddad', 1),
(3, 'Priya Nair', 1),
(4, 'Tom Becker', 2),
(5, 'Lena Fischer', 2),
(6, 'Sam Ortiz', 3),
(7, 'Mia Chen', 5);
SELECT employee_id, name, LEVEL AS depth, SYS_CONNECT_BY_PATH(name, ' / ') AS chain
FROM employees
START WITH manager_id IS NULL
CONNECT BY PRIOR employee_id = manager_id;
Choose Import, then Create Dataflow.
2. Check the rows
The walk is one box, Repeat_1. Choose Run, then click the dot on the right edge of Transform_1 to see the answer.
The rows come in the order Oracle gives them: each person, then everyone under them, before the next person at the same level. Grace is at depth 1, Omar and his team follow, then Priya and hers.
3. Export it
Choose Export from the toolbar at the top right. Pick SQL, then the database under Dialect. The list has PostgreSQL, MySQL, SQL Server, SQLite and Oracle. Tick Include source tables to get the INSERT statements as well.
This is the query written for PostgreSQL. It returns the same seven rows in the same order.
WITH RECURSIVE Repeat_1_source AS (
SELECT employees.employee_id,
employees.name,
employees.manager_id,
CAST(ROW_NUMBER() OVER () + 1000000000 AS TEXT) AS dotty_row
FROM employees
),
Repeat_1 (employee_id, name, manager_id, level_number, path, dotty_level, dotty_chain) AS (
SELECT CAST(Repeat_1_source.employee_id AS DOUBLE PRECISION),
CAST(Repeat_1_source.name AS TEXT),
CAST(Repeat_1_source.manager_id AS DOUBLE PRECISION),
CAST(CAST(1 AS DOUBLE PRECISION) AS DOUBLE PRECISION),
CAST(CONCAT(CAST(NULL AS TEXT), ' / ', Repeat_1_source.name) AS TEXT),
CAST(1 AS DOUBLE PRECISION),
CAST('/' || Repeat_1_source.dotty_row || '/' AS TEXT)
FROM Repeat_1_source
WHERE (CAST(Repeat_1_source.manager_id AS DOUBLE PRECISION) IS NULL)
UNION ALL
SELECT CAST(Repeat_1_source.employee_id AS DOUBLE PRECISION),
CAST(Repeat_1_source.name AS TEXT),
CAST(Repeat_1_source.manager_id AS DOUBLE PRECISION),
CAST(Repeat_1.dotty_level + 1 AS DOUBLE PRECISION),
CAST(CONCAT(Repeat_1.path, ' / ', Repeat_1_source.name) AS TEXT),
CAST(CASE WHEN Repeat_1.dotty_level >= 1000000 THEN CAST(Repeat_1.dotty_chain AS DOUBLE PRECISION) ELSE Repeat_1.dotty_level + 1 END AS DOUBLE PRECISION),
CAST(Repeat_1.dotty_chain || '/' || Repeat_1_source.dotty_row || '/' AS TEXT)
FROM Repeat_1
JOIN Repeat_1_source ON (CAST(Repeat_1.employee_id AS DOUBLE PRECISION) = Repeat_1_source.manager_id)
)
SELECT Repeat_1.employee_id,
Repeat_1.name,
Repeat_1.level_number AS depth,
Repeat_1.path AS chain
FROM Repeat_1
ORDER BY Repeat_1.dotty_chain;
4. Read the result
Each part of the Oracle query has a place in the new one:
- START WITH manager_id IS NULL is the WHERE of the first SELECT, which picks the top of the tree.
- CONNECT BY PRIOR employee_id = manager_id is the JOIN in the second SELECT, which finds the people under each row found so far.
- LEVEL is level_number: 1 at the top, one more at each step down.
- SYS_CONNECT_BY_PATH is path, which adds each name to the path of the row above it.
The rest is there to make the new query give exactly what Oracle gives:
- dotty_row and dotty_chain keep Oracle's order. A recursive WITH query returns its rows in no set order, so each row carries the row numbers of everyone above it, and the query sorts by that.
- The CASE on dotty_level stops a loop in the data, such as two people who manage each other. Oracle refuses such a walk with an error. The new query stops with an error after 1,000,000 levels rather than running for ever.
- The CASTs give every column the same type in both halves of the query, which PostgreSQL and SQL Server require.
Try it
The Recursive Walks and Series example has more walks like this one to run and export.
Open the Recursive Walks and Series example