Explain PL/SQL block structure with the help of diagram.
Of course. Here is a detailed explanation of the PL/SQL block structure, complete with a diagram, a breakdown of its components, and a practical example.
PL/SQL (Procedural Language/Structured Query Language) is Oracle's procedural extension to SQL. The fundamental unit of any PL/SQL program is the Block. A block is a structure that groups related declarations and statements into a single, logical unit. This allows you to write modular, readable, and maintainable code with procedural logic and error handling.
The structure is composed of three distinct sections. Only the BEGIN...END
part is mandatory.
+-------------------------------------------------------------------------+
| |
| DECLARE -- (Optional) The Declaration Section |
| |
| -- This is where you declare variables, constants, cursors, |
| -- user-defined exceptions, etc. that will be used in the block. |
| |
+-------------------------------------------------------------------------+
| |
| BEGIN -- (Mandatory) The Executable Section |
| |
| -- This is the main body of the program where the logic resides. |
| -- It contains both SQL statements (SELECT, INSERT, UPDATE) and |
| -- procedural statements (IF-THEN-ELSE, loops). |
| |
+-------------------------------------------------------------------------+
| |
| EXCEPTION -- (Optional) The Exception-Handling Section |
| |
| -- This section contains the code that handles runtime errors |
| -- (exceptions) that may occur in the BEGIN section. |
| -- It acts as a safety net to prevent the program from crashing. |
| |
+-------------------------------------------------------------------------+
| |
| END; -- (Mandatory) The End of the Block |
| |
+-------------------------------------------------------------------------+
/ -- A forward slash to execute the block in tools like SQL*Plus |
+-------------------------------------------------------------------------+
DECLARE
(The Optional Declaration Section)BEGIN
and EXCEPTION
sections. It's like the "ingredients list" for your program.BEGIN
(The Mandatory Executable Section)NULL;
statement.SELECT
, INSERT
, UPDATE
, DELETE
) and procedural statements (IF
, LOOP
, assignments).EXCEPTION
(The Optional Exception-Handling Section)BEGIN
section. It acts as a safety net to catch errors and allow the program to handle them gracefully instead of crashing.BEGIN
block, the normal execution stops, and control is immediately transferred to the EXCEPTION
block.WHEN NO_DATA_FOUND THEN ...
).END;
(The Mandatory Terminator)END;
statement is mandatory and marks the end of the PL/SQL block.Here is a practical example of an anonymous block (a block without a name) that uses all three sections to find an employee's name based on their ID.
`
sql
-- This command is needed in tools like SQL*Plus or SQL Developer to see the output
SET SERVEROUTPUT ON;
-- The DECLARE section starts here (Optional)
DECLARE
-- Declare a variable to hold the employee's ID we are searching for.
v_employee_id employees.employee_id%TYPE := 101;
-- Declare a variable to hold the full name fetched from the database.
v_full_name VARCHAR2(100);
-- The BEGIN section starts here (Mandatory)
BEGIN
-- This is the main logic.
-- Fetch the first and last name from the employees table into our variable.
SELECT first_name || ' ' || last_name
INTO v_full_name
FROM employees
WHERE employee_id = v_employee_id;
-- If the SELECT is successful, display the result to the screen.
DBMS_OUTPUT.PUT_LINE('Employee Found: ' || v_full_name);
-- The EXCEPTION section starts here (Optional)
EXCEPTION
-- This block runs ONLY if an error occurs in the BEGIN section.
-- This handler catches the specific error when no employee is found with the given ID.
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Error: No employee found with ID ' || v_employee_id);
-- This is a catch-all handler for any other possible errors.
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An unexpected error occurred. Error code: ' || SQLCODE);
-- The END statement marks the end of the block (Mandatory)
END;
/
`
How it works:
1. Declaration: It declares two variables, v_employee_id
and v_full_name
.
2. Execution: It tries to SELECT
the name of the employee with ID 101 into the v_full_name
variable.
3. Success Path: If an employee with ID 101 is found, it prints their name.
4. Error Path: If no employee with that ID exists, the SELECT ... INTO
statement raises a NO_DATA_FOUND
exception. Control immediately jumps to the EXCEPTION
section, and the corresponding error message is printed instead.