Oracle 23.26SQLLab-verified · 2026-11-29

ORA-38104: Columns referenced in the ON Clause cannot be updated

MERGE ... WHEN MATCHED THEN UPDATE tries to change a column that is used in the ON condition.

Oracle AI Database 26ai Free 23.26.3.0.0
ORA-38104: Columns referenced in the ON Clause cannot be updated: "O"."STATUS"

Jump to a cause

  1. 01Status filter in ON, and the same status updated in WHEN MATCHED
  2. 02Updating the key that ON joins on (code conversion)

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

Oracle does not let MERGE update a column that appears in its ON clause. The message names the column (here "O"."STATUS" and "I"."ITEM_CODE"), and the error is raised at parse time, pointing at the ON line, before any row is changed.

Two common ways to hit it: a filter condition put into ON, and a code migration where the key itself changes. Both reproduced on Oracle 23ai.

01Status filter in ON, and the same status updated in WHEN MATCHED

ON (o.order_id = p.order_id AND o.status = 'NEW') then UPDATE SET o.status = 'PAID'. status is in ON, so it cannot be updated.

Setup
Setuprun as lab_a
CREATE TABLE orders (order_id NUMBER PRIMARY KEY, status VARCHAR2(10), amount NUMBER);
CREATE TABLE payments (order_id NUMBER, paid_amount NUMBER);
INSERT INTO orders VALUES (1, 'NEW', 1000);
INSERT INTO orders VALUES (2, 'NEW', 2000);
INSERT INTO orders VALUES (3, 'PAID', 500);
INSERT INTO payments VALUES (1, 1000);
INSERT INTO payments VALUES (3, 500);
COMMIT;
OUTPUT
Table created.


Table created.


1 row created.


1 row created.


1 row created.


1 row created.


1 row created.


Commit complete.
Reproducerun as lab_a
MERGE INTO orders o
USING payments p
ON (o.order_id = p.order_id AND o.status = 'NEW')
WHEN MATCHED THEN UPDATE SET o.status = 'PAID';
✕ ERROR
ON (o.order_id = p.order_id AND o.status = 'NEW')
                                *
ERROR at line 3:
ORA-38104: Columns referenced in the ON Clause cannot be updated: "O"."STATUS"
Help: https://docs.oracle.com/error-help/db/ora-38104/
Checkrun as lab_a
SELECT o.order_id, o.status, p.paid_amount FROM orders o JOIN payments p ON p.order_id = o.order_id ORDER BY o.order_id;
OUTPUT
  ORDER_ID STATUS                         PAID_AMOUNT
---------- ------------------------------ -----------
         1 NEW                                   1000
         3 PAID                                   500

2 rows selected.

Keep only the join key in ON and move the filter to the WHEN MATCHED clause: WHEN MATCHED THEN UPDATE SET o.status = 'PAID' WHERE o.status = 'NEW'. 1 row merged: order 1 became PAID, order 3 (already PAID) was not counted, order 2 (no payment) stayed NEW.

After the fixrun as lab_a
MERGE INTO orders o
USING payments p
ON (o.order_id = p.order_id)
WHEN MATCHED THEN UPDATE SET o.status = 'PAID'
  WHERE o.status = 'NEW';
SELECT order_id, status FROM orders ORDER BY order_id;
ROLLBACK;
✓ SUCCESS
1 row merged.


  ORDER_ID STATUS
---------- ------------------------------
         1 PAID
         2 NEW
         3 PAID

3 rows selected.


Rollback complete.

02Updating the key that ON joins on (code conversion)

Converting old codes to new ones with ON (i.item_code = m.old_code) and SET i.item_code = m.new_code changes the column ON depends on.

Setup
Setuprun as lab_a
CREATE TABLE items (item_code VARCHAR2(10) PRIMARY KEY, item_name VARCHAR2(30));
CREATE TABLE code_map (old_code VARCHAR2(10), new_code VARCHAR2(10));
INSERT INTO items VALUES ('A-001', 'bolt');
INSERT INTO items VALUES ('A-002', 'nut');
INSERT INTO code_map VALUES ('A-001', 'B-001');
INSERT INTO code_map VALUES ('A-002', 'B-002');
COMMIT;
OUTPUT
Table created.


Table created.


1 row created.


1 row created.


1 row created.


1 row created.


Commit complete.
Reproducerun as lab_a
MERGE INTO items i
USING code_map m
ON (i.item_code = m.old_code)
WHEN MATCHED THEN UPDATE SET i.item_code = m.new_code;
✕ ERROR
ON (i.item_code = m.old_code)
    *
ERROR at line 3:
ORA-38104: Columns referenced in the ON Clause cannot be updated: "I"."ITEM_CODE"
Help: https://docs.oracle.com/error-help/db/ora-38104/
Checkrun as lab_a
SELECT i.ROWID AS rid, i.item_code, m.new_code FROM items i JOIN code_map m ON m.old_code = i.item_code;
OUTPUT
RID                ITEM_CODE  NEW_CODE
------------------ ---------- ----------
AAASSZAAYAAAAANAAA A-001      B-001
AAASSZAAYAAAAANAAB A-002      B-002

2 rows selected.

Join on ROWID instead: build the source as SELECT t.ROWID AS rid, m.new_code FROM items t JOIN code_map m ..., then ON (i.ROWID = s.rid). Now item_code is not in ON; 2 rows merged and the codes became B-001, B-002.

After the fixrun as lab_a
MERGE INTO items i
USING (SELECT t.ROWID AS rid, m.new_code
       FROM items t JOIN code_map m ON m.old_code = t.item_code) s
ON (i.ROWID = s.rid)
WHEN MATCHED THEN UPDATE SET i.item_code = s.new_code;
SELECT item_code, item_name FROM items ORDER BY item_code;
ROLLBACK;
✓ SUCCESS
2 rows merged.


ITEM_CODE  ITEM_NAME
---------- ------------------------------
B-001      bolt
B-002      nut

2 rows selected.


Rollback complete.

FAQ

How do I fix ORA-38104?

Remove the updated column from the ON clause. Put filters into WHEN MATCHED THEN UPDATE ... WHERE, or join on ROWID when the key itself must change.

Can MERGE update the column used in the ON condition?

No. Oracle rejects it with ORA-38104 before changing any row. The message shows which column, e.g. "O"."STATUS".

How do I update a primary key or code column with MERGE?

Select the target ROWID together with the new value in the USING subquery and join ON (i.ROWID = s.rid). Then SET i.item_code = s.new_code is allowed.

Is moving the condition to WHEN MATCHED ... WHERE the same result?

Here it updated only rows that were NEW and had a payment (1 row merged); the already PAID row and the unpaid row were left as they were.

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