In Oracle PL/SQL, PRAGMA refers to a compiler directive or "hint" it is used to provide an instruction to the compiler. The directive restricts member subprograms to query or modify database tables and packaged variables. Pragma directives are processed at compile time where they pass necessary information to the compiler; they are not processed at runtime.
The 5 types of Pragma directives available in Oracle are listed below:
- PRAGMA AUTONOMOUS_TRANSACTION: This pragma can perform an autonomous transaction within a PL/SQL block between a BEGIN and END statement without affecting the entire transaction.
- PRAGMA SERIALLY_REUSABLE: This directive tels Oracle that the package state is needed only for the duration of one call to the server. After the call is made the package may be unloaded to reclaim memory.
- PRAGMA RESTRICT_REFRENCES: Defines the purity level of a packaged program. After Oracle8i this is no longer required.
- PRAGMA EXCEPTION_INIT: This directive binds a user defined exception to a particular error number.
- PRAGMA INLINE: (Introduced in Oracle 11g) This directive specifies that a subprogram call either is or is not to be inlined. Inlining replaces a subprogram call with a copy of the called subprogram.
CREATE OR REPLACE [FUNCTION | PROCEDURE] [NAME] IS
Note that PRAGMA resides in the Declarative section of a PL/SQL block.
The procedure P_ERR_LOG uses the PRAGMA AUTONOMOUS_TRANSACTION directive to capture the error occuring in a program unit.
CREATE OR REPLACE PROCEDURE P_ERR_LOG (P_UNIT VARCHAR2, P_SQLCODE NUMBER, P_SQLERRM VARCHAR2)
INSERT INTO ERR_TABLE
VALUES (P_UNIT, P_SQLCODE, P_SQLERRM);