You are on page 1of 3

1) What is Pragma? Pragma is a keyword in Oracle PL/SQL that is used to provide an instruction to the compiler.

This keyword signifies that the statement is a pragma (compiler directive). Pragmas are processed at compile time, not at run time. They do not affect the meaning of a program; they simply convey information to the compiler. Note: Pragmas are defined in the declarative section in PL/SQL. 2) What are the different types of pragma and where can we use them? There are 4 types of Pragmas. They are: 1. 2. 3. 4. pragma AUTONOMOUS_TRANSACTION pragma EXCEPTION_INIT pragma RESTRICT_REFERENCES pragma SERIALLY_REUSABLE

AUTONOMOUS_TRANSACTION: Prior to Oracle 8.1, each Oracle session in PL/SQL could have at most one active transaction at a given time. In other words, changes were all or nothing. Oracle8i PL/SQL addresses that short comings with the AUTONOMOUS_TRANSACTION pragma. This pragma can perform an autonomous transaction within a PL/SQL block between a BEGIN and END statement without affecting the entire transaction. For instance, if rollback or commit needs to take place within the block without effective the transaction outside the block, this type of pragma can be used. The AUTONOMOUS_TRANSACTION pragma instructs the PL/SQL compiler to mark a routine as autonomous (independent). An autonomous transaction is an independent transaction started by another transaction, the main transaction. Autonomous transactions let you suspend the main transaction, do SQL operations, commit or roll back those operations, then resume the main transaction. Example: Create Table Z (A number, B number) --The Prcedure auto2 is marked as autonomous create or replace procedure auto2 as pragma autonomous_transaction;

For more info on Autonomus Transaction Refer: http://download.oracle.com/docs/cd/B10501_01/appdev.920/a96624/13_elems3.htm#327 EXCEPTION_INIT: The most commonly used pragma, this is used to bind a user defined exception to a particular error number. For example: Declare I_GIVE_UP EXCEPTION; PRAGMA EXCEPTION_INIT(I_GIVE_UP, -20000); BEGIN . EXCEPTION WHEN I_GIVE_UP do something.. END; RESTRICT_REFERENCES: Defines the purity level of a packaged program. This is not required starting with Oracle8i. Prior to Oracle8i if you were to invoke a function within a package specification from a SQL statement, you would have to provide a RESTRICT_REFERENCE directive to the PL/SQL engine for that function. This pragma confirms to Oracle database that the function as the specified side-effects or ensures that it lacks any such side-effects. Usage is as follows: PRAGMA RESTRICT_REFERENCES(function_name, WNDS [, WNPS] [, RNDS], [, RNPS]) WNDS: Writes No Database State. States that the function will not perform any DMLs. WNPS: Writes No Package State. States that the function will not modify any Package variables. RNDS: Reads No Database State. Analogous to Write. This pragma affirms that the function will not read any database tables.

RNPS: Reads No Package State. Analogous to Write. This pragma affirms that the function will not read any package variables. For more info on RESTRICT_REFERENCES Refer: http://download.oracle.com/docs/cd/B10501_01/appdev.920/a96624/08_subs.htm#17077 SERIALLY_REUSABLE: This pragma lets the PL/SQL engine know that package-level data should not persist between reference to that data. Package data (global variables in package specification etc.) by default persists for an entire session (or until a package is recompiled). Globally accessible data structures can cause some side effects. For instance, what if a cursor is left open in a package. In addition, a program can use up lots of real memory (UGA) and then not release it if the data is stored in a package-level structure. In order to manage this, Oracle8i introduced the SERIALLY_REUSABLE pragma. This pragma is used in packages only and must be defined BOTH in specification and in the body. The advantage is that based on the pragma, a package state can be reduced to a single call of a program unit in the package as opposed to the package being available for the whole session.

You might also like