Oracle 23.26SQLLab-verified · 2026-11-29

ORA-01436: CONNECT BY loop in user data

A CONNECT BY query followed the data in a circle and came back to a row it had already visited.

Oracle AI Database 26ai Free 23.26.3.0.0
ORA-01436: CONNECT BY loop in user data

Jump to a cause

  1. 01Manager chain loops back to the top (A reports to B, B to C, C to A)
  2. 02A row whose parent and child are the same (BOM self-reference)

Lab-verifiedReproduced and fixed on Oracle AI Database 26ai Free 23.26.3.0.0 · 2026-11-29 · container image gvenzl/oracle-free:23-slim

ORA-01436 means the hierarchy in your table is not a tree. Following PRIOR id = mgr_id (or PRIOR child = parent) eventually leads back to a row that is already on the current path, so Oracle stops the query.

The query itself is usually fine; the data has a loop. Add NOCYCLE and select CONNECT_BY_ISCYCLE to see where the loop is, then fix the data or keep NOCYCLE. Both cases below were run on Oracle 26ai Free.

01Manager chain loops back to the top (A reports to B, B to C, C to A)

In emp, the CEO (id 1) has mgr_id = 3, Kim (2) reports to 1, and Lee (3) reports to 2. Starting at id 1 with CONNECT BY PRIOR id = mgr_id goes 1 → 2 → 3 → 1, and the query fails with ORA-01436.

Running the same query with CONNECT BY NOCYCLE and CONNECT_BY_ISCYCLE returned all 4 rows, and Lee (id 3) had IS_CYCLE = 1: his child in the hierarchy is the starting row again. The real error was the CEO having a manager, so UPDATE emp SET mgr_id = NULL WHERE id = 1 fixed it. The original query then returned 4 rows at levels 1, 2, 3, 3.

Setup
Setuprun as lab_a
CREATE TABLE emp (id NUMBER PRIMARY KEY, name VARCHAR2(20), mgr_id NUMBER);
INSERT INTO emp VALUES (1, 'CEO', 3);
INSERT INTO emp VALUES (2, 'Kim', 1);
INSERT INTO emp VALUES (3, 'Lee', 2);
INSERT INTO emp VALUES (4, 'Park', 2);
COMMIT;
OUTPUT
Table created.


1 row created.


1 row created.


1 row created.


1 row created.


Commit complete.
Reproducerun as lab_a
SELECT LEVEL, id, name FROM emp
START WITH id = 1
CONNECT BY PRIOR id = mgr_id;
✕ ERROR
ERROR:
ORA-01436: CONNECT BY loop in user data
Help: https://docs.oracle.com/error-help/db/ora-01436/



no rows selected
Checkrun as lab_a
SELECT id, name, mgr_id, CONNECT_BY_ISCYCLE AS is_cycle FROM emp
START WITH id = 1
CONNECT BY NOCYCLE PRIOR id = mgr_id;
OUTPUT
        ID NAME                               MGR_ID   IS_CYCLE
---------- ------------------------------ ---------- ----------
         1 CEO                                     3          0
         2 Kim                                     1          0
         3 Lee                                     2          1
         4 Park                                    2          0

4 rows selected.
Fixrun as lab_a
UPDATE emp SET mgr_id = NULL WHERE id = 1;
COMMIT;
OUTPUT
1 row updated.


Commit complete.

CONNECT_BY_ISCYCLE can only be used together with NOCYCLE. It marks the row whose child would close the loop.

After the fixrun as lab_a
SELECT LEVEL, id, name FROM emp
START WITH id = 1
CONNECT BY PRIOR id = mgr_id;
✓ SUCCESS
     LEVEL         ID NAME
---------- ---------- ------------------------------
         1          1 CEO
         2          2 Kim
         3          3 Lee
         3          4 Park

4 rows selected.

02A row whose parent and child are the same (BOM self-reference)

The bom table has a row BOARD → BOARD. Walking down from PC with CONNECT BY PRIOR child = parent reaches BOARD and then BOARD again, so it fails with ORA-01436. One bad row is enough.

SELECT parent, child FROM bom WHERE parent = child found that row. With CONNECT BY NOCYCLE the query returned 2 rows (PC → BOARD, BOARD → CPU), and IS_CYCLE was 1 on the PC → BOARD row.

Setup
Setuprun as lab_a
CREATE TABLE bom (parent VARCHAR2(10), child VARCHAR2(10), qty NUMBER);
INSERT INTO bom VALUES ('PC', 'BOARD', 1);
INSERT INTO bom VALUES ('BOARD', 'CPU', 1);
INSERT INTO bom VALUES ('BOARD', 'BOARD', 1);
COMMIT;
OUTPUT
Table created.


1 row created.


1 row created.


1 row created.


Commit complete.
Reproducerun as lab_a
SELECT LEVEL, parent, child FROM bom
START WITH parent = 'PC'
CONNECT BY PRIOR child = parent;
✕ ERROR
ERROR:
ORA-01436: CONNECT BY loop in user data
Help: https://docs.oracle.com/error-help/db/ora-01436/



no rows selected
Checkrun as lab_a
SELECT parent, child FROM bom WHERE parent = child;
OUTPUT
PARENT     CHILD
---------- ----------
BOARD      BOARD

1 row selected.

NOCYCLE only skips the loop; the BOARD → BOARD row is still in the table.

After the fixrun as lab_a
SELECT LEVEL, parent, child, CONNECT_BY_ISCYCLE AS is_cycle FROM bom
START WITH parent = 'PC'
CONNECT BY NOCYCLE PRIOR child = parent;
✓ SUCCESS
     LEVEL PARENT     CHILD        IS_CYCLE
---------- ---------- ---------- ----------
         1 PC         BOARD               1
         2 BOARD      CPU                 0

2 rows selected.

FAQ

How do I find which rows cause CONNECT BY loop in user data?

Run the same query with CONNECT BY NOCYCLE and add CONNECT_BY_ISCYCLE to the select list. Rows with 1 are where the loop closes. For a direct self-reference, WHERE parent = child finds it.

Does NOCYCLE fix ORA-01436?

It stops the error and returns the rows up to the loop, but the looping data is still there. Fix the data if the loop is a mistake.

Why did my hierarchical query work yesterday and fail with ORA-01436 today?

Someone changed the data. On this page one UPDATE (the top row getting a manager) or one inserted row (parent = child) was enough to break the query.

Can I use CONNECT_BY_ISCYCLE without NOCYCLE?

No. It is only valid when the CONNECT BY clause has NOCYCLE.

How we verify

Each scenario starts from fresh database users. A script runs the setup, confirms the exact error code, applies the fix and runs the statement again. The output shown is what the database printed, saved by the script.

lab_a and lab_b are ordinary users created for the test; system is the DBA account. Oracle AI Database 26ai Free Release 23.26.3.0.0 - Develop, Learn, and Run for Free 23.26.3.0.0