Sub topic 8
Assigning Values to Variables
Assume you know basic SQL and can run PL/SQL in SQL*Plus, SQLcl, or SQL Developer. The examples use the sample HR.EMPLOYEES table.
A PL/SQL variable receives a value in two common ways:
- An assignment statement uses
:=. - A
SELECT INTOstatement copies values returned by a query.
The assignment operator is :=, not =. The right side is evaluated first, and the result is stored in the variable on the left.
Assignment with `:=`
You can assign a literal, another variable, or an expression. An expression combines values and operators to produce a result.
SET SERVEROUTPUT ON
DECLARE
v_price NUMBER(10,2) := 100;
v_total NUMBER(10,2);
BEGIN
v_total := v_price * 1.075;
DBMS_OUTPUT.PUT_LINE('Total: ' || v_total);
END;
/The declaration gives v_price its initial value. The executable section calculates v_total. The value 107.5 is then joined to the text in the DBMS_OUTPUT.PUT_LINE call.
A variable can be assigned more than once:
SET SERVEROUTPUT ON
DECLARE
v_status VARCHAR2(20);
BEGIN
v_status := 'Started';
DBMS_OUTPUT.PUT_LINE(v_status);
v_status := 'Finished';
DBMS_OUTPUT.PUT_LINE(v_status);
END;
/The second assignment replaces the first value. If an expression contains NULL, the result is usually NULL. For example, 10 + NULL produces NULL.
Loading values with `SELECT INTO`
SELECT INTO assigns query results to variables. The INTO variables appear between the select list and the FROM clause.
SET SERVEROUTPUT ON
DECLARE
v_last_name employees.last_name%TYPE;
v_salary employees.salary%TYPE;
BEGIN
SELECT last_name, salary
INTO v_last_name, v_salary
FROM employees
WHERE employee_id = 100;
DBMS_OUTPUT.PUT_LINE(v_last_name || ' earns ' || v_salary);
END;
/The first selected expression goes into v_last_name. The second goes into v_salary. Their order must match.
A SELECT INTO statement used this way must return exactly one row. No matching row raises NO_DATA_FOUND. More than one matching row raises TOO_MANY_ROWS. Handle those exceptions when the data is not guaranteed to be unique.
Matching types and avoiding errors
Use %TYPE when a variable should match a table column. This reduces conversion errors and keeps the declaration aligned with the schema.
For a direct assignment, the source and target should be compatible. Oracle can perform some implicit conversions, but relying on them can cause errors when formats or session settings change.
| Error | Typical cause | Better approach |
|---|---|---|
| `PLS-00382` | Incompatible expression or target type | Use compatible types or explicit conversion |
| `ORA-01403` | `SELECT INTO` found no row | Handle `NO_DATA_FOUND` |
| `ORA-01422` | `SELECT INTO` found multiple rows | Restrict the query or handle `TOO_MANY_ROWS` |
A declaration initializer runs when the block starts. It is different from an assignment in the executable section, which runs when execution reaches that statement.
Exercise
Write the assignment statement that increases v_salary by 10 percent and stores the result in v_new_salary.
Show answer
v_new_salary := v_salary * 1.10;