Sub topic 5
Declaring and Naming PL/SQL Variables
This chapter assumes that you can run an Oracle anonymous block and understand basic Oracle types such as NUMBER, VARCHAR2, and DATE. It also assumes that you can use SQL*Plus, SQL Developer, or SQLcl.
A PL/SQL variable is a named storage location for a value. You declare it in the declaration section of a block, before the BEGIN keyword. The declaration gives the variable a name and a data type.
The declaration syntax
A basic variable declaration has this form:
variable_name data_type;For example:
DECLARE
v_customer_name VARCHAR2(100);
v_order_total NUMBER(10, 2);
v_order_date DATE;
BEGIN
NULL;
END;
/The declaration section can contain several declarations. Each declaration ends with a semicolon. The executable section starts after BEGIN.
A variable has the value NULL when you declare it without an initial value. NULL means that the value is unknown or absent. It is different from an empty string or from zero.
You can assign an initial value when you declare the variable:
DECLARE
v_status VARCHAR2(20) := 'PENDING';
v_count NUMBER := 0;
BEGIN
NULL;
END;
/The := operator assigns a value in PL/SQL. Do not use = for assignment. The equals sign compares values in conditions.
Naming variables clearly
Oracle allows letters, numbers, underscores, and some other characters in identifiers, but variable names should be easy to read. A common convention is to prefix local variables with v_.
Useful naming conventions include:
v_for a local variable, such asv_salaryc_for a constant, such asc_tax_rate- A descriptive noun or noun phrase, such as
v_employee_name - A name that describes the value rather than its storage format
Avoid names such as x, data, or temp unless the meaning is obvious from a very small block. Clear names make errors easier to find.
Oracle does not distinguish between uppercase and lowercase letters in ordinary unquoted identifiers. These names refer to the same identifier:
v_salary
V_SALARY
V_SalaryUse one style consistently. Lowercase names with underscores are common in PL/SQL source code.
Oracle also has naming rules that limit ordinary identifiers. A name should begin with a letter, should not contain spaces, and should not be a reserved word such as BEGIN, SELECT, or DATE.
Choosing a compatible data type
Choose a type that can represent every value the variable needs to hold.
| Value | Suitable type | Example |
|---|---|---|
| Whole or decimal number | `NUMBER` | `v_quantity NUMBER` |
| Text | `VARCHAR2` | `v_name VARCHAR2(100)` |
| Date and time | `DATE` | `v_hired_on DATE` |
| True or false in PL/SQL logic | `BOOLEAN` | `v_is_active BOOLEAN` |
The precision and scale in NUMBER(10, 2) describe the maximum number of digits and the digits after the decimal point. VARCHAR2(100) reserves a maximum length of 100 characters for the value.
Declaring and using variables
The following block declares variables, assigns values, and prints them:
SET SERVEROUTPUT ON
DECLARE
v_employee_name VARCHAR2(100) := 'Ada Lovelace';
v_monthly_pay NUMBER(10, 2) := 8500.00;
v_hire_date DATE := DATE '2026-01-15';
BEGIN
DBMS_OUTPUT.PUT_LINE('Employee: ' || v_employee_name);
DBMS_OUTPUT.PUT_LINE('Monthly pay: ' || v_monthly_pay);
DBMS_OUTPUT.PUT_LINE('Hire date: ' || TO_CHAR(v_hire_date, 'YYYY-MM-DD'));
END;
/DBMS_OUTPUT.PUT_LINE displays text in clients that have server output enabled. The || operator joins text values. Oracle converts the number to text when it concatenates the value in this example.
Exercise
What is wrong with this declaration: v_total = NUMBER(10,2);?
Show answer
It uses = instead of the declaration syntax. The correct declaration is v_total NUMBER(10,2);.