Sub topic 13
Handling `NO_DATA_FOUND` and `TOO_MANY_ROWS`
This chapter assumes that you know basic SELECT statements, Oracle data types, and how to run an anonymous PL/SQL block in SQL*Plus, SQL Developer, or SQLcl. It also assumes access to a table such as HR.EMPLOYEES.
Why `SELECT INTO` raises exceptions
A PL/SQL SELECT INTO statement reads query results into variables. Unlike a regular SQL query, it expects exactly one row.
| Query result | PL/SQL result |
|---|---|
| Exactly one row | Variables receive the column values |
| No rows | `NO_DATA_FOUND` is raised |
| More than one row | `TOO_MANY_ROWS` is raised |
The exception stops the normal executable statements. PL/SQL then looks for a matching handler in the EXCEPTION section.
Handling `NO_DATA_FOUND`
The following block searches for an employee ID that does not exist. The SELECT INTO statement raises NO_DATA_FOUND, so the output statement in the executable section is skipped.
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('Employee: ' || v_last_name);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No employee matched that ID.');
END;
/The handler contains a WHEN clause followed by the predefined exception name. The handler can display a message, set a fallback value, record an error, or take another appropriate action.
Do not use NO_DATA_FOUND to hide a real data problem. If the application requires an employee to exist, a missing employee may need to be reported to the caller rather than treated as normal.
Handling `TOO_MANY_ROWS`
TOO_MANY_ROWS occurs when the query returns two or more rows. For example, a department can contain several employees, so selecting an employee by department_id may not identify one row.
SET SERVEROUTPUT ON
DECLARE
v_last_name employees.last_name%TYPE;
BEGIN
SELECT last_name
INTO v_last_name
FROM employees
WHERE department_id = 50;
DBMS_OUTPUT.PUT_LINE('Employee: ' || v_last_name);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('The department has no employees.');
WHEN TOO_MANY_ROWS THEN
DBMS_OUTPUT.PUT_LINE('The department has multiple employees.');
END;
/This block is valid even when the data changes. If department 50 has one employee, the name is printed. If it has none or several, the corresponding handler runs.
To avoid TOO_MANY_ROWS, change the requirement or the query. You might select by employee_id, add a condition that makes the result unique, or use a cursor when the code must process several rows. Adding ROWNUM = 1 only hides the data problem unless any matching row is genuinely acceptable.
Exception flow
The exception section handles errors raised in the executable section. Once a handler runs, control leaves that block. Statements after the failing statement are not executed.
Choosing a handler
Handle the specific exceptions that the block can reasonably encounter. A broad WHEN OTHERS handler can catch unexpected errors, but it should not replace specific handlers. If you use it, log or re-raise the error instead of silently ignoring it.
Exercise
Which exception is raised when a SELECT INTO query returns three rows?
Show answer
TOOMANYROWS.