Lab-verifiedReproduced and fixed on Oracle AI Database 26ai Free 23.26.3.0.0 · 2026-11-30 · container image gvenzl/oracle-free:23-slim
ORA-14551 means a SQL query called a PL/SQL function, and that function changed data. Oracle does not allow DML while a query is running, so the whole SELECT fails. The ORA-06512 line under it names the function and line, for example at "LAB_A.GET_SAL", line 4.
The fix depends on what the DML is for. Below are two cases reproduced on Oracle Database 26ai Free with the fix that was actually run.
01Function writes a log row and is called in SELECT
get_sal inserts a row into access_log and then returns the salary. Calling it as SELECT ename, get_sal(empno) FROM emp failed with ORA-14551 at line 4, the INSERT.
When the log must be written even inside a query, make the function an autonomous transaction: add PRAGMA AUTONOMOUS_TRANSACTION and COMMIT after the INSERT. After recreating it, the same SELECT returned KIM 3000 and LEE 4000, and access_log had 2 rows.
Setup
CREATE TABLE emp (empno NUMBER PRIMARY KEY, ename VARCHAR2(20), sal NUMBER);
INSERT INTO emp VALUES (1, 'KIM', 3000);
INSERT INTO emp VALUES (2, 'LEE', 4000);
CREATE TABLE access_log (empno NUMBER, read_at DATE);
COMMIT;
CREATE OR REPLACE FUNCTION get_sal(p_empno NUMBER) RETURN NUMBER IS
v_sal NUMBER;
BEGIN
INSERT INTO access_log VALUES (p_empno, SYSDATE);
SELECT sal INTO v_sal FROM emp WHERE empno = p_empno;
RETURN v_sal;
END;
/Table created.
1 row created.
1 row created.
Table created.
Commit complete.
Function created.SELECT ename, get_sal(empno) AS sal FROM emp;SELECT ename, get_sal(empno) AS sal FROM emp
*
ERROR at line 1:
ORA-14551: cannot perform a DML operation inside a query
ORA-06512: at "LAB_A.GET_SAL", line 4
Help: https://docs.oracle.com/error-help/db/ora-14551/CREATE OR REPLACE FUNCTION get_sal(p_empno NUMBER) RETURN NUMBER IS
PRAGMA AUTONOMOUS_TRANSACTION;
v_sal NUMBER;
BEGIN
INSERT INTO access_log VALUES (p_empno, SYSDATE);
COMMIT;
SELECT sal INTO v_sal FROM emp WHERE empno = p_empno;
RETURN v_sal;
END;
/Function created.An autonomous transaction commits on its own, separately from the caller. Use it only for logs or audit rows that should stay even if the main work rolls back.
SELECT ename, get_sal(empno) AS sal FROM emp;
SELECT COUNT(*) AS log_rows FROM access_log;ENAME SAL
-------------------- ----------
KIM 3000
LEE 4000
2 rows selected.
LOG_ROWS
----------
2
1 row selected.02Numbering function (UPDATE) called with SELECT ... FROM dual
next_doc_no increases doc_seq.last_no with UPDATE and returns the new number. SELECT next_doc_no('ORD') FROM dual failed with ORA-14551, and last_no stayed at 100.
A function that changes data should be called from PL/SQL, not from a query. Assigning it in a block (v_no := next_doc_no('ORD');) and committing printed doc_no = 101.
Setup
CREATE TABLE doc_seq (doc_type VARCHAR2(10) PRIMARY KEY, last_no NUMBER);
INSERT INTO doc_seq VALUES ('ORD', 100);
COMMIT;
CREATE OR REPLACE FUNCTION next_doc_no(p_type VARCHAR2) RETURN NUMBER IS
v_no NUMBER;
BEGIN
UPDATE doc_seq SET last_no = last_no + 1 WHERE doc_type = p_type
RETURNING last_no INTO v_no;
RETURN v_no;
END;
/Table created.
1 row created.
Commit complete.
Function created.SELECT next_doc_no('ORD') AS doc_no FROM dual;SELECT next_doc_no('ORD') AS doc_no FROM dual
*
ERROR at line 1:
ORA-14551: cannot perform a DML operation inside a query
ORA-06512: at "LAB_A.NEXT_DOC_NO", line 4
Help: https://docs.oracle.com/error-help/db/ora-14551/SELECT doc_type, last_no FROM doc_seq;DOC_TYPE LAST_NO
---------- ----------
ORD 100
1 row selected.Do not use an autonomous transaction here: the number would be committed even if the document insert fails. If you only need unique numbers, a sequence is simpler.
SET SERVEROUTPUT ON
DECLARE
v_no NUMBER;
BEGIN
v_no := next_doc_no('ORD');
DBMS_OUTPUT.PUT_LINE('doc_no = ' || v_no);
COMMIT;
END;
/doc_no = 101
PL/SQL procedure successfully completed.FAQ
What does ORA-14551 cannot perform a DML operation inside a query mean?
A function used inside a SELECT (or WHERE, ORDER BY) ran INSERT, UPDATE, DELETE or MERGE. Oracle stops the query with ORA-14551 and the ORA-06512 line shows where.
How do I call a function that does an INSERT from a SELECT?
Add PRAGMA AUTONOMOUS_TRANSACTION to the function and COMMIT inside it. In the first case the SELECT then worked and the log rows were saved.
Is PRAGMA AUTONOMOUS_TRANSACTION always the right fix?
No. It commits separately from the caller, so it suits logs. For business data like document numbers, call the function from a PL/SQL block instead, as in the second case.
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