ORA-14551: unable to perform DML operations in queries

Source: Internet
Author: User
Recently, I failed to debug a function with DML operations. It was okay during PLSQL testing and failed to call the function through SQL statements. I didn't find the cause at the beginning.

Recently, I failed to debug a function with DML operations. It was okay during PL/SQL testing. I couldn't call the function through SQL statements. I didn't find the reason at the beginning.

Recently, when debugging a function with DML operations, it has never been successful. It is okay to test in PL/SQL. It will not work when calling a function through SQL statements, at the beginning has not found the cause, then inadvertently caught in the function code comment out, finally through SQL debugging, a pop-up "ORA-14551: unable to execute DML operations in the query. "error, find the cause of the problem, and find a solution. I found an article on the Internet to talk about autonomous transactions and main transactions. After reading this article for a long time, I am still in the fog and find the key points, it is to add a statement "PRAGMA AUTONOMOUS_TRANSACTION;" and submit the DML operation at the end of the COMMIT statement. The problem can be solved. As for the autonomous transaction and the main transaction, there is still time to digest it later.

---- The following is a reference:

When inserting data into a temporary table in a function, the following error occurs:

ORA-14551: unable to perform DML operations in queries

ORA-06512: In "NSTCSA. NS_ST_GETRAISEFUNDX", line 29

Add the following string:

PRAGMA AUTONOMOUS_TRANSACTION;


A database transaction is a unit operation. Either all operations are successful or all operations fail. In Oracle, a transaction starts from executing the first data management language (DML) statement until it executes a COMMIT statement, commits and saves the transaction, or executes a ROLLBACK statement, stop this operation.

It is difficult to record the error information to the database table because the transaction fails to be re-run, the INSERT statement used to write log entries has not been completed yet.

To address this dilemma, Oracle provides a convenient method, that is, autonomous transactions. An autonomous transaction starts from the current transaction and runs in its own context. They can be submitted or re-run independently without affecting running transactions. As a result, they form an ideal form of writing error log tables. When an error is detected in a transaction, you can insert a row in the error log table and submit it, and then roll back the primary transaction without losing this insert.

Because the autonomous transaction is separated from the primary transaction, it cannot detect the current status of the modified row. It seems that they are always in separate sessions before the primary transaction is committed, and they are unavailable for autonomous transactions. However, in turn, the situation is different: the main transaction can detect the results of self-governing transactions that have been executed.

To create an autonomous transaction, you must use the PRAGMA AUTONOMOUS_TRANSACTION statement in PL/SQL at the top of the anonymous block or in the stored procedure, function, data packet, or trigger definition section. The SQL Server statements executed in such a module or process are autonomous.

The trigger cannot contain the COMMIT statement, unless the PRAGMA AUTONOMOUS_TRANSACTION flag exists. However, only the statements in the trigger can be committed, but not the primary transaction.

Exp:


Create table Msg (Msg varchar (50 ));

Homemade transactions:
Create or replace procedure AutoNomouse_Insert is

PRAGMA AUTONOMOUS_TRANSACTION;

Begin

Insert into Msg values ('autonomouse insert ');

Commit;

End;
Non-autonomous transactions:

Create or replace Procedure NonAutoNomouse_Insert

Begin

Insert into Msg Values ('nonautonomouse insert ');

Commit;

End;


SQL> begin
2
3 insert into Msg Values ('this Main info ');
4
5 NonAutoNomouse_Insert;
6
7 rollback;
8
9 end
10;
11/

PL/SQL procedure successfully completed

SQL> select * from msg;

MSG
--------------------------------------------------
This Main Info
NonAutonomouse Insert
Because there is a COMMIT in the process, RULLBACK in the anonymous block does not work. Therefore, the ROLLBACK will affect the entire transaction in a non-autonomous transaction.

Let's look at another situation:
SQL> delete msg;

2 rows deleted

SQL>

There is no COMMIT here;


SQL> begin
2
3 insert into Msg Values ('this Main info ');
4
5 rollback; -- ROLLBACK is added here;
6
7 NonAutoNomouse_Insert;
8
9 rollback;
10
11 end
12;
13/

PL/SQL procedure successfully completed

SQL> select * from msg;

MSG
--------------------------------------------------
This Main Info
NonAutonomouse Insert
NonAutonomouse Insert

Why is there no ROLLBACK (DELETE * from ssg? Because the process is a new SESSION, the previous SESSION is normally exited and automatically submitted at the same time. Therefore, we can see three rows of data.
SQL> commit;

Commit complete

SQL> select * from msg;

MSG
--------------------------------------------------
This Main Info
NonAutonomouse Insert
NonAutonomouse Insert

SQL> commit;

Commit complete

SQL> select * from msg;

MSG
--------------------------------------------------
This Main Info
NonAutonomouse Insert
NonAutonomouse Insert

A new SESSION is a meaningless transaction control statement.

SQL> delete msg;

3 rows deleted

SQL> commit;

Commit complete

SQL> select * from msg;

MSG
--------------------------------------------------

We can see that the submission is normal;

Let's take a look at the self-made transactions:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.