Sub topic 10
Loading Variables with SELECT INTO
This chapter assumes that you know basic SELECT statements, Oracle data types, and how to run an anonymous PL/SQL block in SQL Developer, SQL*Plus, or SQLcl. It also assumes access to the sample HR.EMPLOYEES table.
What SELECT INTO Does
A PL/SQL SELECT INTO statement runs a query and stores its result in one or more PL/SQL variables. Unlike a normal SQL query, it must return exactly one row.
The INTO clause goes between the select list and the FROM clause:
SELECT column_1, column_2
INTO variable_1, variable_2
FROM table_name
WHERE condition;The first selected value goes into the first variable. The second selected value goes into the second variable. Their order must match.
A Single-Row Query
This block reads the last name and salary for employee 100:
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 variables use %TYPE, so their data types follow the corresponding table columns. The query must find one employee with employee_id = 100.
The assignment happens when the SELECT INTO statement runs. You do not use := for this assignment. You use := when assigning an expression or another PL/SQL value directly.
Query Result Rules
The query result determines whether the statement succeeds.
| Query result | PL/SQL behavior |
|---|---|
| Exactly one row | Values are assigned to the variables |
| No rows | Raises `NO_DATA_FOUND` |
| More than one row | Raises `TOO_MANY_ROWS` |
A condition that looks unique to you is not enough. The database must actually return one row. For example, selecting by department_id may return many employees, so it is unsafe when assigning to one scalar variable.
If you need a summary of many rows, an aggregate function such as COUNT or MAX usually returns one row:
DECLARE
v_employee_count PLS_INTEGER;
BEGIN
SELECT COUNT(*)
INTO v_employee_count
FROM employees
WHERE department_id = 90;
DBMS_OUTPUT.PUT_LINE('Employees: ' || v_employee_count);
END;
/Handling Expected Exceptions
When no row is found, or when the condition is less specific than expected, handle the exception explicitly:
SET SERVEROUTPUT ON
DECLARE
v_last_name employees.last_name%TYPE;
BEGIN
SELECT last_name
INTO v_last_name
FROM employees
WHERE employee_id = -1;
DBMS_OUTPUT.PUT_LINE(v_last_name);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No employee matched the ID.');
WHEN TOO_MANY_ROWS THEN
DBMS_OUTPUT.PUT_LINE('The ID matched multiple employees.');
END;
/The exception section runs only when an earlier statement raises an error. After the matching handler runs, execution continues after the complete block.
Selecting a Whole Row
When several columns from one row are needed, %ROWTYPE creates a record with fields matching the table:
DECLARE
v_employee employees%ROWTYPE;
BEGIN
SELECT *
INTO v_employee
FROM employees
WHERE employee_id = 100;
DBMS_OUTPUT.PUT_LINE(
v_employee.first_name || ' ' || v_employee.last_name
);
END;
/The query's selected columns must match the record structure. A SELECT * into a table %ROWTYPE record is convenient, but selecting named columns is often clearer and less sensitive to table changes.
Exercise
What exception does SELECT INTO raise when its query returns three rows?
Show answer
It raises TOOMANYROWS because SELECT INTO requires exactly one row.