You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
Every piece of PL/SQL code is organised into a block — a self-contained unit with a defined structure. Understanding the block is the foundation of everything else in PL/SQL.
DECLARE
-- Optional: variable declarations, cursor declarations, type definitions
BEGIN
-- Mandatory: executable statements (SQL + procedural logic)
EXCEPTION
-- Optional: error handlers
END;
/
| Section | Required? | Purpose |
|---|---|---|
| DECLARE | No | Declare local variables, cursors, and types |
| BEGIN | Yes | The code that actually runs |
| EXCEPTION | No | Handle runtime errors gracefully |
| END | Yes | Closes the block |
BEGIN
DBMS_OUTPUT.PUT_LINE('Hello, PL/SQL!');
END;
/
DBMS_OUTPUT.PUT_LINE writes a line of text to the output buffer. In SQL Developer, enable output with View → DBMS Output. In SQL*Plus, run SET SERVEROUTPUT ON first.
DECLARE
v_message VARCHAR2(100) := 'Hello, World!';
v_count NUMBER := 0;
BEGIN
v_count := v_count + 1;
DBMS_OUTPUT.PUT_LINE(v_message);
DBMS_OUTPUT.PUT_LINE('Count: ' || v_count);
END;
/
Blocks can be nested inside other blocks. The inner block can access variables from the outer block, but not vice versa:
DECLARE
v_outer VARCHAR2(20) := 'outer';
BEGIN
DECLARE
v_inner VARCHAR2(20) := 'inner';
BEGIN
DBMS_OUTPUT.PUT_LINE(v_outer || ' and ' || v_inner);
END;
-- v_inner is not accessible here
DBMS_OUTPUT.PUT_LINE(v_outer);
END;
/
An anonymous block has no name — you type it and run it directly. It cannot be called by other code.
A named block (procedure, function, package) is stored in the database and can be called by name. You'll cover those in later lessons.
-- Single-line comment
/*
Multi-line
comment
*/
PL/SQL uses || (double pipe) to concatenate strings:
DBMS_OUTPUT.PUT_LINE('Value is: ' || v_count);
; after each statement/ at the end to run the blockSET SERVEROUTPUT ON in SQL*Plus (output won't appear)Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.