PL/SQL Anonymous Blocks
A PL/SQL anonymous block is an unnamed, nonpersistent PL/SQL unit.
Typical uses for anonymous blocks include:
Initiating calls to subprograms and package constructs
Isolating exception handling
Managing control by nesting code within other PL/SQL blocks
Anonymous blocks do not have the code reuse advantages of stored subprograms. Table 8-1 summarizes the differences between the two types of PL/SQL units.
Table 8-1 Differences Between Anonymous Blocks and Subprograms
| Is the PL/SQL Unit ... | Anonymous Blocks | Subprograms |
|---|---|---|
Specified with a name? | No | Yes |
Compiled with every reuse? | No | No |
Stored in the database? | No | Yes |
Invocable by other applications? | No | Yes |
Capable of returning bind variable values? | Yes | Yes |
Capable of returning function values? | No | Yes |
Capable of accepting parameters? | No | Yes |
An anonymous block consists of an optional declarative part, an executable part, and one or more optional exception handlers. The following sample anonymous block selects an employee last name into a variable and prints the name:
DECLARE v_lname VARCHAR2(25); BEGIN SELECT last_name INTO v_lname FROM employees WHERE employee_id = 101; DBMS_OUTPUT.PUT_LINE('Employee last name is '||v_lname); END;
Oracle Database compiles the PL/SQL block and places it in the shared pool of the SGA, but it does not store the source code or compiled version in the database for reuse beyond the current instance. Unlike triggers, an anonymous block is compiled each time it is loaded into memory. Shared SQL allows anonymous PL/SQL blocks in the shared pool to be reused and shared until they are flushed out of the shared pool.
Comments
Post a Comment