Sub topic 4
The Declaration Section
Assume you know basic SQL and Oracle data types, and can run a PL/SQL block in SQL*Plus, SQL Developer, or SQLcl. This chapter focuses on the part of a block that defines the data it will use.
Where declarations fit
A PL/SQL block has up to three sections:
The declaration section begins with DECLARE and ends when BEGIN starts. It is optional, but BEGIN is required. Declare variables, constants, cursors, and local types before the executable statements that use them.
A variable is a named storage location whose value can change while the block runs. A constant is a named value that cannot change after initialization.
An anonymous block has no stored name. You can run it immediately, which makes it useful for testing declarations and short administrative tasks.
Declaring variables
A basic declaration contains a name, a data type, and an optional initial value:
SET SERVEROUTPUT ON
DECLARE
v_message VARCHAR2(50) := 'Hello, PL/SQL';
v_count PLS_INTEGER := 3;
BEGIN
DBMS_OUTPUT.PUT_LINE(v_message);
DBMS_OUTPUT.PUT_LINE('Count: ' || v_count);
END;
/The := operator assigns a value. In a declaration, it supplies the variable's initial value. You can also use DEFAULT:
DECLARE
v_status VARCHAR2(10) DEFAULT 'OPEN';
BEGIN
NULL;
END;
/A variable without an initial value starts as NULL. NULL means “unknown or absent,” not zero or an empty string.
Choose a type that matches the value:
| Type | Typical use |
|---|---|
| `NUMBER` | Exact integers and decimal numbers |
| `VARCHAR2(n)` | Text with a maximum length |
| `DATE` | Date and time values |
| `BOOLEAN` | `TRUE`, `FALSE`, or `NULL` inside PL/SQL |
| `PLS_INTEGER` | Integer calculations and counters |
Use a clear naming convention, such as v_ for variables and c_ for constants. A declaration can include precision and scale, as in NUMBER(10,2), but values assigned later must still fit those limits.
Constants and required values
Declare a constant with the CONSTANT keyword. A constant must receive a value in its declaration.
The NOT NULL constraint prevents a variable from holding NULL. A NOT NULL variable must also be initialized immediately.
SET SERVEROUTPUT ON
DECLARE
c_tax_rate CONSTANT NUMBER(4,3) := 0.075;
v_price NUMBER(10,2) NOT NULL := 100;
v_total NUMBER(10,2);
BEGIN
v_total := v_price * (1 + c_tax_rate);
DBMS_OUTPUT.PUT_LINE('Total: ' || v_total);
END;
/This block prints Total: 107.5. The constant protects the tax rate from accidental reassignment. The v_price declaration guarantees that the price has a value before the executable section begins.
Declarations are processed when the block starts. If an initialization expression fails, the block does not reach its normal executable statements. Keep initialization expressions simple and make dependencies clear.
Declaration order and scope
A declaration can refer to an earlier declaration, but not normally to one that appears later. For example, declare v_price before a variable whose default expression uses v_price.
Each declaration is visible within its scope. A variable declared in an outer block is available to nested blocks. A variable declared in an inner block is unavailable outside that block. If both blocks use the same name, the inner declaration shadows the outer one.
SET SERVEROUTPUT ON
DECLARE
v_level VARCHAR2(10) := 'outer';
BEGIN
DECLARE
v_level VARCHAR2(10) := 'inner';
BEGIN
DBMS_OUTPUT.PUT_LINE(v_level);
END;
DBMS_OUTPUT.PUT_LINE(v_level);
END;
/The output is inner followed by outer. Use distinct names when possible. If shadowing is intentional, qualify the outer variable with a block label.
Exercise
What happens if a constant is declared without an initial value, or a NOT NULL variable is declared without one?
Show answer
The block fails during compilation because both declarations require an initial value.