Inserts in an Oracle function will report Ora-14551:cannot perform a DML operation inside a query
problem Reason:
Functions that write to the database (INSERT, UPDATE, DELETE, create, alter, commit) cannot be invoked simply by using SQL
Workaround:
Need to declare add pragma autonomous_transaction;
For example:
Create or Replace function update_contractdate (V_contractid in Contract.id%type, v_contractbegindatestr in varchar2)
return number
As
pragma autonomous_transaction;
PRAGMA Autonomous_transaction declared as an autonomous transaction the current function runs as a child transaction of an existing transaction, and the commit,rollback operation of the child transaction does not affect the state of the parent transaction
One or more autonomous transactions can be defined in one transaction, and autonomous transactions can be commit independently, without affecting the outer transaction, and the rollback of the same outer transaction has no effect on the autonomous transaction, and it is generally possible to consider defining an autonomous transaction as a process, called in a transaction in the outer layer.