8.13 PL/SQL Functions that SQL Statements Can Invoke
To be invocable from SQL statements, a stored function (and any subprograms that it invokes) must obey the following purity rules, which are meant to control side effects:
-
When invoked from a
SELECTstatement or a parallelizedINSERT,UPDATE,DELETE, orMERGEstatement, the subprogram cannot modify any database tables. -
When invoked from an
INSERT,UPDATE,DELETE, orMERGEstatement, the subprogram cannot query or modify any database tables modified by that statement.If a function either queries or modifies a table, and a DML statement on that table invokes the function, then ORA-04091 (mutating-table error) occurs. There is one exception: ORA-04091 does not occur if a single-row
INSERTstatement that is not in aFORALLstatement invokes the function in aVALUESclause. -
When invoked from a
SELECT,INSERT,UPDATE,DELETE, orMERGEstatement, the subprogram cannot execute any of the following SQL statements (unlessPRAGMAAUTONOMOUS_TRANSACTIONwas specified):-
Transaction control statements (such as
COMMIT) -
Session control statements (such as
SETROLE) -
System control statements (such as
ALTERSYSTEM) -
Database definition language (DDL) statements (such as
CREATE), which are committed automatically
(For the description of
PRAGMAAUTONOMOUS_TRANSACTION, see "AUTONOMOUS_TRANSACTION Pragma".) -
If any SQL statement in the execution part of the function violates a rule, then a runtime error occurs when that statement is parsed.
The fewer side effects a function has, the better it can be optimized in a SELECT statement, especially if the function is declared with the option DETERMINISTIC or PARALLEL_ENABLE (for descriptions of these options, see "DETERMINISTIC Clause" and "PARALLEL_ENABLE Clause").
See Also:
-
Oracle Database Development Guide for information about restrictions on PL/SQL functions that SQL statements can invoke