Sub topic 1
Anatomy of a PL/SQL Block
PL/SQL is Oracle's procedural language. A PL/SQL block groups declarations, executable statements, and optional error handling into one unit. The block is the basic structure used by anonymous scripts, stored procedures, functions, and triggers.
Prerequisites
You should know basic SQL, including SELECT, and understand Oracle types such as NUMBER and VARCHAR2. You should also be able to run code in SQL*Plus, SQLcl, or SQL Developer. This chapter assumes access to an Oracle database, although the first example does not need a table.
The three sections of a block
A block can have up to three sections:
- The declaration section begins with
DECLARE. It defines variables, constants, cursors, and local subprograms. This section is optional. - The executable section begins with
BEGIN. It contains the statements that PL/SQL runs. This section is required. - The exception section begins with
EXCEPTION. It handles errors raised by the executable section. This section is optional. - The block ends with
END.
The declaration section comes before BEGIN. The executable section comes after it. If an exception section exists, it comes between the executable statements and END.
Each PL/SQL statement ends with a semicolon. The final slash on a line by itself is different. It tells a client such as SQL*Plus to send the complete block to Oracle. The slash is not part of the PL/SQL language itself.
An anonymous block
An anonymous block has no stored name. You submit it, Oracle runs it, and its definition is not saved as a database object.
SET SERVEROUTPUT ON
DECLARE
v_message VARCHAR2(50) := 'Hello, PL/SQL';
BEGIN
DBMS_OUTPUT.PUT_LINE(v_message);
END;
/The declaration creates v_message, a variable that can hold up to 50 characters. The := operator assigns its initial value. The executable section calls DBMS_OUTPUT.PUT_LINE, which writes a line of text to the client.
SET SERVEROUTPUT ON enables display of that output in SQL*Plus and SQLcl. In SQL Developer, enable the DBMS Output panel and associate it with the database connection when needed.
The block runs in this order:
- Oracle creates the local variable and gives it its initial value.
- Oracle executes the statement between
BEGINandEND. - Oracle finishes the block and discards the local variable.
A declaration is not an instruction that runs from top to bottom. It describes the data that the executable section can use. Execution starts at BEGIN.
Named PL/SQL units
A named unit is stored in the database and can be run again. Procedures and functions are common examples. Triggers are also named PL/SQL units, although Oracle runs them in response to database events.
The same block structure appears inside these units. A stored procedure might have a header before the declaration section:
CREATE OR REPLACE PROCEDURE say_hello AS
v_message VARCHAR2(50) := 'Hello from a procedure';
BEGIN
DBMS_OUTPUT.PUT_LINE(v_message);
END;
/The procedure is compiled and stored under the name SAY_HELLO. Its declaration and executable sections still follow the same rules as an anonymous block. A procedure can later be called with:
BEGIN
say_hello;
END;
/This second block is anonymous. It calls the named procedure from its executable section.
Normal completion and errors
If every executable statement succeeds, the block reaches END and completes normally. If a statement raises an error and the block has an EXCEPTION section, Oracle searches that section for a matching handler. The handler can display a message, undo work, or raise the error again.
The exception section belongs to the block that handles the error. A later chapter will examine specific exceptions such as NO_DATA_FOUND and TOO_MANY_ROWS.
Exercise
Which sections of a PL/SQL block are required, and what does the slash after END do?
Show answer
The executable section beginning with BEGIN is required. DECLARE and EXCEPTION are optional. The slash tells the client to submit the completed block to Oracle.