DOTTY

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.

The Import SQL window with the CONNECT BY query in it.

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.

The dataflow employees, Repeat_1, Transform_1, with Transform_1's output open: seven rows with employee_id, name, depth and chain, from Grace Lin at depth 1 to Sam Ortiz at depth 3.

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.

The Export panel with SQL chosen and POSTGRESQL as the dialect.

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:

The rest is there to make the new query give exactly what Oracle gives:

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