Sub topic 2
Anonymous Blocks and Named Blocks
This chapter assumes you know basic SQL, Oracle data types, and how to run statements in SQL*Plus, SQLcl, or SQL Developer. The examples use the HR.EMPLOYEES table. If your session is not connected to the HR schema, qualify the table as HR.EMPLOYEES.
A PL/SQL block is a unit of executable code. It can declare variables, run SQL and PL/SQL statements, handle errors, and call other program units. The main block sections are:
- The declaration section, introduced by
DECLAREorIS - The executable section, introduced by
BEGIN - The exception section, introduced by
EXCEPTION
The declaration and exception sections are optional. The executable section is required.
Anonymous blocks
An anonymous block has no name and is not stored in the database. You send it to Oracle, Oracle compiles and runs it, and the block then disappears.
SET SERVEROUTPUT ON
DECLARE
v_message VARCHAR2(50) := 'Hello, PL/SQL';
BEGIN
DBMS_OUTPUT.PUT_LINE(v_message);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('The block failed.');
END;
/DBMS_OUTPUT.PUT_LINE writes a line to the client output buffer. SET SERVEROUTPUT ON tells SQL*Plus, SQLcl, and compatible tools to display that buffer.
The semicolon after each statement belongs to PL/SQL syntax. The slash on a line by itself is a client command. It tells the client to send the completed block to Oracle. Do not confuse the slash with part of the PL/SQL language.
Use an anonymous block for one-time work, testing, administrative scripts, or a small operation that does not need to be called later.
Named blocks
A named block is stored in the database with a name. Procedures and functions are common named PL/SQL units. Triggers and package subprograms are other examples. A named unit can be compiled once and called many times.
The following procedure accepts an employee ID and prints the employee's last name and salary:
SET SERVEROUTPUT ON
CREATE OR REPLACE PROCEDURE show_employee (
p_employee_id IN employees.employee_id%TYPE
) IS
v_last_name employees.last_name%TYPE;
v_salary employees.salary%TYPE;
BEGIN
SELECT last_name, salary
INTO v_last_name, v_salary
FROM employees
WHERE employee_id = p_employee_id;
DBMS_OUTPUT.PUT_LINE(
v_last_name || ' earns ' || v_salary
);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No employee was found.');
WHEN TOO_MANY_ROWS THEN
DBMS_OUTPUT.PUT_LINE('The query returned too many employees.');
END show_employee;
/The procedure name is show_employee. Its parameter, p_employee_id, is an input parameter because it is declared with IN. The IS keyword begins the procedure's declaration section. The procedure ends with END show_employee;.
After creating the procedure, call it from another block:
BEGIN
show_employee(100);
END;
/Oracle stores the procedure's compiled definition. If the table structure or referenced objects change, the procedure can become invalid and may need recompilation.
Choosing between the two forms
| Question | Anonymous block | Named procedure or function |
|---|---|---|
| Stored in the database? | No | Yes |
| Can be called repeatedly? | Only by sending it again | Yes |
| Has a database object name? | No | Yes |
| Best use | One-time scripts and experiments | Reusable application logic |
A named unit gives you reuse and a clear interface. An anonymous block is simpler when the code will run only once. Both forms use the same core block structure.
Exercise
Which form should you choose for a one-time script that reads one employee and prints a message, and which form should you choose for reusable employee-report logic? Explain why.
Show answer
Use an anonymous block for the one-time script because it does not need to remain in the database. Use a named procedure for reusable reporting logic because Oracle stores it and other blocks or applications can call it repeatedly.