PL/SQL uses BLOCK structure as its basic structure. Each PL/SQL program consists of SQL and PL/SQL statement which form a PL/SQL block.
PL/SQL block contains 3 sections.
- The Declaration Section (optional)
- The Execution Section (mandatory)
- The Exception handling Section (optional)
The basic structure of a PL/SQL (Procedural Language/Structured Query Language) block consists of three sections:
- Declaration Section: This is where you declare variables, constants, and cursors that will be used in the block. It starts with the keyword
DECLARE
and ends with the keywordBEGIN
. - Execution Section: This is where you write the actual PL/SQL code or logic. It starts with the keyword
BEGIN
and ends with the keywordEND
. - Exception Section: This section handles exceptions (errors) that may occur during the execution of the block. It starts with the keyword
EXCEPTION
and ends with the keywordEND
.
Here is a simple example:
DECLARE
-- Declaration Section
v_variable NUMBER := 10;
BEGIN— Execution Section
DBMS_OUTPUT.PUT_LINE(‘The value of v_variable is: ‘ || v_variable);
— Exception Section
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE(‘An error occurred: ‘ || SQLERRM);
END;
In this example, DECLARE
declares a variable v_variable
, BEGIN
contains the PL/SQL code, and EXCEPTION
handles any exceptions that might occur. The /
at the end is a delimiter indicating the end of the PL/SQL block.