Sub topic 1
Recap: the PL/SQL Block as the Building Unit
PL/SQL is Oracle's procedural language. A PL/SQL block groups declarations, executable statements, and error-handling statements into one unit. This chapter assumes you know basic SQL and anonymous blocks with DECLARE, BEGIN, EXCEPTION, and END.
Named procedures and functions use the same block structure as anonymous blocks. The main difference is that Oracle stores a named block in the database so that you can call it again.
Prerequisites
You should be able to write basic SELECT, INSERT, UPDATE, and DELETE statements. You should also understand Oracle types such as NUMBER, VARCHAR2, and DATE, and know how an anonymous PL/SQL block runs.
The block structure
A PL/SQL block has three sections:
| Section | Purpose | Required? |
|---|---|---|
| Declaration | Defines variables, constants, and local exceptions | No |
| Execution | Runs the procedural statements | Yes |
| Exception | Handles errors raised during execution | No |
The execution section starts with BEGIN and ends with END. Even a block with no declarations must include these keywords.
DECLARE
v_total NUMBER := 0;
BEGIN
SELECT SUM(salary)
INTO v_total
FROM employees
WHERE department_id = 60;
DBMS_OUTPUT.PUT_LINE('Total salary: ' || NVL(v_total, 0));
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Unexpected error: ' || SQLERRM);
END;
/The slash is a client command. Tools such as SQL*Plus and SQLcl use it to send the completed PL/SQL block to Oracle. It is not part of the PL/SQL language itself.
Nested blocks and scope
A block can contain another block. The inner block can use names declared in its outer block. The outer block cannot use names declared only inside the inner block.
DECLARE
v_department_id NUMBER := 60;
BEGIN
DECLARE
v_count NUMBER;
BEGIN
SELECT COUNT(*)
INTO v_count
FROM employees
WHERE department_id = v_department_id;
DBMS_OUTPUT.PUT_LINE('Employees: ' || v_count);
END;
END;
/The variable v_department_id belongs to the outer block, so the nested block can read it. The variable v_count belongs to the nested block and disappears when that block ends.
> callout note > Keep declarations close to the block that owns them. Narrow scope makes code easier to read and reduces accidental name conflicts.
From anonymous blocks to named subprograms
An anonymous block is compiled and run as one operation. A stored subprogram is a named PL/SQL block saved in the database. A procedure performs an action. A function computes and returns a value.
Both forms can contain local variables, SQL statements, nested blocks, and exception handlers. A stored subprogram also has a header that defines its name, parameters, and, for a function, its return type.
The block is therefore the foundation for the rest of this tutorial. The next chapters add names, parameters, return values, and reusable interfaces to this structure.
Exercise
Which section of a PL/SQL block must always be present?
Show answer
The executable section, which starts with BEGIN and ends with END.