Articles Comments

Oracle DBA & All IT » Database, DBA, Function, ORA-Error, Oracle, Programming » ORA-14551 cannot perform a DML operation inside a query

ORA-14551 cannot perform a DML operation inside a query

      Error… Error… มาแล้วจ้า..  อิอิ เสียงตามสายจาก SA ที่รักถามไถ่มาว่า ทำไมๆๆ เรียกใช้ Function ที่ด้านในมี Update Statement ทำไมถึง Error

ORA-14551: cannot perform a DML operation inside a query

เอาไปหา ORA-Error เบื้องต้นก็ได้ solution มาตามนี้ เอาจริงๆ อ่านเองก็ยังงง T_T

The Oracle docs note this on the ora-14551 error:

ORA-14551 cannot perform a DML operation inside a query 

Cause: DML operation like insert, update, delete or select-for-update cannot be performed inside a query or under a PDML slave.
 
Action: Ensure that the offending DML operation is not performed or use an autonomous transaction to perform the DML operation within the query or PDML slave.

Internet sources offer information regarding ORA-14551.  Here are the details of the error:

    เราก็ไม่เคยเจอเหมือนกัน เพราะไม่ค่อยได้เขียน function ก็เอา Error ไปถามกับพี่กรู (google) ก้ถึงบางอ้อ….. อ๋อ function ไม่ได้เขียนอะไรผิด แต่วิธีการเรียกใช้จะต้องเปลี่ยน Syntax นิดหน่อย เรามาลองดูตัวอย่างด้านล่างกัน

สร้าง Myfunction ตามนี้ มี update ด้านใน

create or replace function myFunction return varchar2 as
begin
update emp set empno = empno +1 where empno = 0;
return 'Yeah';
end;
/

แล้วลอง insert data ลงไปซักหน่อย

SQL> insert into emp (empno, ename) values (0, 'TEST');
1 row created.

แล้วลองเรียกด้วยวิธีเดิมๆ ที่เคยชิน จ๊าก..ก เจอ Error เข้าไปเต็มๆ

SQL> var myVar VARCHAR2
SQL> SELECT myFunction INTO :myVar FROM DUAL;
SELECT myFunction INTO :myVar FROM DUAL
*
ERROR at line 1:

ORA-14551: cannot perform a DML operation inside a query
ORA-06512: at "SCOTT.MYFUNCTION", line 3
ORA-06512: at line 1

วิธีแก้ไขไม่ยากเลย แค่เปลี่ยน ชีวิตก็เปลี่ยน อิอิ

เปลี่ยนไปเรียกใช้ syntax ตามนี้:

SQL> var myVar VARCHAR2
SQL> call myFunction() INTO :myVar;
Call completed.

Remark:
1. โดย ORA-14551 จะเกิดเฉพาะกับ Function ที่มีการ (INSERT, UPDATE or DELETE) 
2. error ORA-14551 จะเกิดใน 9i ขึ้นไป ใน 8i จะได้ Error ORA-6571 แทน

 

 

.. Preview : 5383

Related Search:

  • ora-14551

Written by

บอกเล่าสิ่งที่พบเจอมาในการทำงาน ประสบการณ์การทำงานด้าน DBA ถ่ายทอดกันด้วยภาษาง่ายๆ บ้านๆ " ทุกอย่างไม่อยาก แต่... แค่ตั้งใจไม่พอ ต้องลงมือทำ และทำ GoodLuck " Fanpage: www.facebook.com/DBAor .. "Oracle Database Consultant " ..

Filed under: Database, DBA, Function, ORA-Error, Oracle, Programming · Tags: , , , , , , , ,

  • Hey blogger, nice work with the choice of theme on this blog. It looks reall nice.

  • Yeah, that’s the tciekt, sir or ma’am