In this article
- What ORA-01403 means and when Oracle raises it
- Sample setup
- Reproduce the error
- Fix 1: handle NO_DATA_FOUND with a default value
- Fix 2: raise a clear business error instead
- Fix 3: use a statement that does not raise the error
- ORA-01403 no data found but data exists
- Why a function in a SELECT returns NULL instead of ORA-01403
- ORA-01403 from an associative array
- Common mistakes
- Best practices
- Frequently asked questions
- Summary
ORA-01403: no data found means a SELECT ... INTO statement in PL/SQL returned zero rows. SELECT INTO must put exactly one row into its variables. When the WHERE clause matches nothing, PL/SQL raises the predefined exception NO_DATA_FOUND and, if nothing handles it, your block stops with ORA-01403.
The fix is almost never to hide the error. You decide what "no row" means for your code: a default value, a clear business error, or simply skipping the record. Then you write that decision into an exception handler or use a statement that does not raise the error. This guide shows each option with runnable examples, and explains the confusing case where you get ORA-01403 even though the data seems to exist.
What ORA-01403 means and when Oracle raises it
NO_DATA_FOUND is one of the predefined exceptions that PL/SQL declares in package STANDARD. Its SQLCODE is +100, not a negative number like most errors. You will usually see it together with ORA-06512, which tells you the line where it happened.
The most common source is SELECT INTO. The Oracle docs say it directly: if the SELECT INTO statement returns no rows, PL/SQL raises NO_DATA_FOUND. If it returns more than one row you get a different error, ORA-01422 (TOO_MANY_ROWS).
Many statements that look similar do not raise it. Knowing which is which removes most of the confusion:
| Statement or action | What happens when nothing matches |
|---|---|
| SELECT ... INTO variable | Raises NO_DATA_FOUND (ORA-01403) |
| EXECUTE IMMEDIATE ... INTO variable | Raises NO_DATA_FOUND (ORA-01403) |
| Reading a missing element of an associative array | Raises NO_DATA_FOUND (ORA-01403) |
| SELECT COUNT(*) or MAX(...) INTO variable | No error. Returns 0 or NULL |
| SELECT ... BULK COLLECT INTO collection | No error. The collection is empty (COUNT = 0) |
| FETCH from an explicit cursor | No error. cursor%NOTFOUND becomes TRUE |
| UPDATE or DELETE that matches no rows | No error. SQL%ROWCOUNT is 0 |
| PL/SQL function called from a SQL query | No error. The function result is NULL |
The last row surprises a lot of developers. It is covered in its own section below.
Sample setup
Create two small tables. Note two details on purpose: emp_code is a CHAR column, and one hire date has a time part. Both are used later to explain the "data exists but ORA-01403" case. Employee 104 points to department 40, which does not exist.
CREATE TABLE departments (
dept_id NUMBER PRIMARY KEY,
dept_name VARCHAR2(30) NOT NULL
);
CREATE TABLE employees (
emp_id NUMBER PRIMARY KEY,
emp_code CHAR(6),
emp_name VARCHAR2(50) NOT NULL,
dept_id NUMBER,
salary NUMBER(10,2),
hire_date DATE
);
INSERT INTO departments VALUES (10, 'Sales');
INSERT INTO departments VALUES (20, 'Finance');
INSERT INTO departments VALUES (30, 'IT');
INSERT INTO employees VALUES (101, 'E101', 'Asha Rao', 10, 52000, DATE '2023-04-10');
INSERT INTO employees VALUES (102, 'E102', 'Ravi Kumar', 20, 61000, DATE '2022-11-01');
INSERT INTO employees VALUES (103, 'E103', 'Meena Iyer', 30, 75000, DATE '2021-07-19');
INSERT INTO employees VALUES (104, 'E104', 'John Mathew', 40, 48000,
TO_DATE('2024-01-15 09:30', 'YYYY-MM-DD HH24:MI'));
COMMIT;
SET SERVEROUTPUT ONReproduce the error
This anonymous block looks up employee 999, who does not exist:
DECLARE
l_name employees.emp_name%TYPE;
BEGIN
SELECT emp_name INTO l_name
FROM employees
WHERE emp_id = 999;
DBMS_OUTPUT.PUT_LINE('Name: ' || l_name);
END;
/ERROR at line 1:
ORA-01403: no data found
ORA-06512: at line 4Read the stack from the bottom. ORA-06512: at line 4 points at the SELECT (line 1 is DECLARE). The PUT_LINE never ran, because the exception jumped straight out of the block. In Oracle Database 23ai the message may also include a help link, but the codes are the same.
Fix 1: handle NO_DATA_FOUND with a default value
If a missing row is normal for your logic, catch the exception right where the query runs and set a sensible value. Wrap the query in its own small BEGIN ... END block so the rest of the code continues.
DECLARE
l_name employees.emp_name%TYPE;
BEGIN
BEGIN
SELECT emp_name INTO l_name
FROM employees
WHERE emp_id = 999;
EXCEPTION
WHEN NO_DATA_FOUND THEN
l_name := 'Unknown';
END;
DBMS_OUTPUT.PUT_LINE('Name: ' || l_name);
END;
/Name: Unknown
PL/SQL procedure successfully completed.The inner block keeps the handler close to the statement it protects. If you put the handler at the end of a long procedure instead, you cannot tell which of several queries failed, and the remaining work after the failed query is skipped.
Skip missing rows inside a loop
The same pattern lets a loop carry on past bad IDs. Here SYS.ODCINUMBERLIST, a number collection type supplied by Oracle, holds the IDs to process:
DECLARE
l_ids SYS.ODCINUMBERLIST := SYS.ODCINUMBERLIST(101, 999, 103);
l_name employees.emp_name%TYPE;
BEGIN
FOR i IN 1 .. l_ids.COUNT LOOP
BEGIN
SELECT emp_name INTO l_name
FROM employees
WHERE emp_id = l_ids(i);
DBMS_OUTPUT.PUT_LINE(l_ids(i) || ': ' || l_name);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE(l_ids(i) || ': not found, skipped');
END;
END LOOP;
END;
/101: Asha Rao
999: not found, skipped
103: Meena Iyer
PL/SQL procedure successfully completed.Fix 2: raise a clear business error instead
Sometimes a missing row really is a problem, for example when a caller passes an employee ID that should exist. Do not swallow it. Convert the vague ORA-01403 into a message that tells the caller what went wrong, with RAISE_APPLICATION_ERROR.
CREATE OR REPLACE FUNCTION get_salary (p_emp_id IN NUMBER)
RETURN NUMBER
IS
l_salary employees.salary%TYPE;
BEGIN
SELECT salary INTO l_salary
FROM employees
WHERE emp_id = p_emp_id;
RETURN l_salary;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE_APPLICATION_ERROR(-20010,
'Employee ' || p_emp_id || ' not found');
END;
/
SELECT get_salary(999) FROM dual;ORA-20010: Employee 999 not found
ORA-06512: at "APP.GET_SALARY", line 12The caller now gets an error code in the -20000 to -20999 range reserved for applications, and a message with the actual ID. Your schema name will appear instead of APP. As the next sections show, this also matters when the function is called from SQL.
Fix 3: use a statement that does not raise the error
Option A: an aggregate function
An aggregate such as MAX or COUNT always returns exactly one row, even when no rows match. The Oracle docs suggest COUNT(*) for this reason.
DECLARE
l_name employees.emp_name%TYPE;
BEGIN
SELECT MAX(emp_name) INTO l_name
FROM employees
WHERE emp_id = 999;
DBMS_OUTPUT.PUT_LINE('Name: ' || NVL(l_name, 'Unknown'));
END;
/Name: Unknown
PL/SQL procedure successfully completed.Option B: an explicit cursor and %NOTFOUND
A FETCH from an explicit cursor never raises ORA-01403. It sets %NOTFOUND to TRUE instead, and you test it with a normal IF.
DECLARE
CURSOR c_emp IS
SELECT emp_name FROM employees WHERE emp_id = 999;
l_name employees.emp_name%TYPE;
BEGIN
OPEN c_emp;
FETCH c_emp INTO l_name;
IF c_emp%NOTFOUND THEN
DBMS_OUTPUT.PUT_LINE('No employee with that ID');
ELSE
DBMS_OUTPUT.PUT_LINE('Name: ' || l_name);
END IF;
CLOSE c_emp;
END;
/No employee with that ID
PL/SQL procedure successfully completed.This is more code than SELECT INTO with a handler, and it does not complain if more than one row matches. Prefer it when you really want "the first row, if any" logic.
Option C: BULK COLLECT for zero or more rows
If the query can legitimately return zero, one or many rows, fetch into a collection. The docs state that PL/SQL does not raise an exception when a BULK COLLECT statement returns no rows, so you must check the collection yourself.
DECLARE
TYPE t_names IS TABLE OF employees.emp_name%TYPE;
l_names t_names;
BEGIN
SELECT emp_name BULK COLLECT INTO l_names
FROM employees
WHERE dept_id = 50;
DBMS_OUTPUT.PUT_LINE('Rows found: ' || l_names.COUNT);
END;
/Rows found: 0
PL/SQL procedure successfully completed.ORA-01403 no data found but data exists
This is the most searched version of the problem. You run the query in SQL Developer or SQL*Plus, it returns a row, yet the PL/SQL code fails. Work through these causes in order.
1. The row is not committed, or you are in a different session
Other sessions cannot see rows you inserted until you commit. If you inserted test data in one SQL Developer tab and run the procedure from another connection or a job, the procedure sees nothing. Commit, then retry.
2. You are connected to a different schema or database
The same table name can exist in more than one schema, and in a multitenant setup in more than one pluggable database. The procedure runs with its owner's tables by default (definer's rights), not necessarily the ones you checked. Confirm where you are:
SELECT USER,
SYS_CONTEXT('USERENV', 'CURRENT_SCHEMA') AS current_schema,
SYS_CONTEXT('USERENV', 'CON_NAME') AS container
FROM dual;3. A CHAR column compared with a VARCHAR2 variable
This one is subtle. emp_code is CHAR(6), so Oracle stores 'E101' padded with two trailing spaces. In plain SQL, WHERE emp_code = 'E101' finds the row, because both sides are CHAR or a text literal and Oracle uses blank-padded comparison. In PL/SQL you often compare with a VARCHAR2 variable, and then Oracle uses nonpadded comparison, so 'E101 ' is not equal to 'E101'.
DECLARE
l_code VARCHAR2(6) := 'E101';
l_name employees.emp_name%TYPE;
BEGIN
SELECT emp_name INTO l_name
FROM employees
WHERE emp_code = l_code;
DBMS_OUTPUT.PUT_LINE('Name: ' || l_name);
END;
/ERROR at line 1:
ORA-01403: no data found
ORA-06512: at line 5Fix it by declaring the variable with the column type, l_code employees.emp_code%TYPE. The value is then padded to six characters and the comparison matches. Avoid wrapping the column in RTRIM, because a function on the column stops Oracle from using a normal index on it. For new tables, use VARCHAR2 rather than CHAR.
4. A DATE column with a time part
Oracle DATE always stores a time. John's hire date is 15 January 2024 at 09:30, so WHERE hire_date = DATE '2024-01-15' matches nothing, because that literal means midnight. Your tool may only display the date part, which hides the difference.
SELECT emp_name
FROM employees
WHERE hire_date >= DATE '2024-01-15'
AND hire_date < DATE '2024-01-16';EMP_NAME
-----------
John MathewThe range finds every time on that day and can still use an index on hire_date. TRUNC(hire_date) = DATE ... also works, but it needs a function-based index to be fast.
5. The error came from a different statement
The failing query may not be the one you tested. Look at every ORA-06512 line in the stack. The top one is where the exception was raised, for example inside a function called by your procedure. In your own error logging, call DBMS_UTILITY.FORMAT_ERROR_BACKTRACE inside the handler so you keep that line number.
6. Case, spaces or a bind value you did not expect
'asha rao' does not equal 'Asha Rao', and a value read from a screen may carry a trailing space. Print the exact value you query with, for example DBMS_OUTPUT.PUT_LINE('[' || l_code || ']'), so hidden spaces show up between the brackets.
Why a function in a SELECT returns NULL instead of ORA-01403
Here is a function with no exception handler. It looks up a department name:
CREATE OR REPLACE FUNCTION get_dept_name (p_dept_id IN NUMBER)
RETURN VARCHAR2
IS
l_name departments.dept_name%TYPE;
BEGIN
SELECT dept_name INTO l_name FROM departments WHERE dept_id = p_dept_id;
RETURN l_name;
END;
/
SELECT emp_name, get_dept_name(dept_id) AS dept_name
FROM employees
ORDER BY emp_id;| EMP_NAME | DEPT_NAME |
|---|---|
| Asha Rao | Sales |
| Ravi Kumar | Finance |
| Meena Iyer | IT |
| John Mathew | (null) |
John's department 40 does not exist, so the function raised NO_DATA_FOUND. The query did not fail. The SQL engine treats NO_DATA_FOUND from a function like an empty result and returns NULL for that row. Oracle Support documents this behaviour (Doc ID 285496.1). Call the same function from PL/SQL and you get the error:
DECLARE
l_dept departments.dept_name%TYPE;
BEGIN
l_dept := get_dept_name(40);
DBMS_OUTPUT.PUT_LINE('Dept: ' || l_dept);
END;
/ERROR at line 1:
ORA-01403: no data found
ORA-06512: at "APP.GET_DEPT_NAME", line 6
ORA-06512: at line 4So the same function behaves differently depending on who calls it. Make the behaviour explicit inside the function: return a default on purpose, or raise an application error as in Fix 2, which does reach the SQL caller. For reports like this one, an outer join is usually the better choice anyway, because SQL does the lookup in one pass instead of running a query per row:
SELECT e.emp_name, d.dept_name
FROM employees e
LEFT JOIN departments d ON d.dept_id = e.dept_id
ORDER BY e.emp_id;ORA-01403 from an associative array
Reading an associative array (INDEX BY table) element that was never assigned also raises NO_DATA_FOUND. There is no SELECT anywhere, which makes this one hard to spot.
DECLARE
TYPE t_dept_map IS TABLE OF VARCHAR2(30) INDEX BY PLS_INTEGER;
l_depts t_dept_map;
BEGIN
l_depts(10) := 'Sales';
l_depts(20) := 'Finance';
DBMS_OUTPUT.PUT_LINE(l_depts(40));
END;
/ERROR at line 1:
ORA-01403: no data found
ORA-06512: at line 7Check the key with the EXISTS method before reading it: IF l_depts.EXISTS(40) THEN ... ELSE ... END IF;. EXISTS returns FALSE for a missing key instead of raising an error.
Common mistakes
WHEN OTHERS THEN NULL. It hides ORA-01403 and every other error, including real bugs. CatchNO_DATA_FOUNDby name and handle only that.- Testing
SQL%NOTFOUNDafterSELECT INTO. The exception is raised before your IF runs, so the check never helps. Use a handler, or a cursor FETCH where%NOTFOUNDdoes work. - Running COUNT(*) first, then SELECT INTO. Two queries do the work of one, and another session can delete the row between them. One
SELECT INTOwith a handler is simpler and correct. - Expecting UPDATE or DELETE to raise ORA-01403. They never do. Check
SQL%ROWCOUNT = 0right after the statement if "no rows changed" is an error for you. - One handler at the bottom of a long procedure. You lose track of which query failed, and all the work after it is skipped. Put small handlers around the queries that can legitimately find nothing.
- Returning NULL silently from lookup functions. Callers cannot tell "not found" from "value is NULL". Document the contract, or raise a clear error.
Best practices
- Decide per query whether "no row" is normal (use a default) or a bug (raise an application error). Write that decision down in the handler.
- Use
%TYPEfor variables you compare with columns. It prevents the CHAR versus VARCHAR2 trap and keeps lengths in sync. - Make sure the column in a single-row lookup is indexed, ideally a primary key or unique key. Then
SELECT INTOis a fast index lookup, and duplicates cannot cause ORA-01422. - Log
DBMS_UTILITY.FORMAT_ERROR_BACKTRACEwhen you re-raise, so the original line number is not lost. - Prefer joins over row-by-row lookup functions in queries. They are faster and do not hide errors.
Frequently asked questions
What is ORA-01403 no data found in Oracle?
It is the error behind the predefined PL/SQL exception NO_DATA_FOUND (SQLCODE +100). It is raised when SELECT INTO or EXECUTE IMMEDIATE ... INTO returns no rows, or when code reads an associative array element that does not exist.
How do I handle the no data found exception in Oracle?
Add EXCEPTION WHEN NO_DATA_FOUND THEN ... to the block that contains the query, ideally a small inner block around just that query. In the handler, assign a default, skip the record, or call RAISE_APPLICATION_ERROR with a clear message.
What does ORA-01403 with ORA-06512 mean?
ORA-01403 is the actual error. ORA-06512 lines are the call stack: each one names the program unit and line number the error passed through. The first ORA-06512 after ORA-01403 shows where the failing statement is.
Does BULK COLLECT raise NO_DATA_FOUND?
No. A BULK COLLECT that returns no rows leaves the collection empty and raises nothing. Check collection.COUNT = 0 yourself.
Why do I get ORA-01403 when the data exists?
The usual causes are uncommitted data, a different schema or container, a CHAR column compared with a VARCHAR2 variable, a DATE with a time part, or the error coming from a different query than the one you tested. Check the ORA-06512 line first.
Summary
- ORA-01403 means
SELECT INTOfound no row, or a missing associative array key was read. - Read the first ORA-06512 line to find the failing statement.
- Handle
NO_DATA_FOUNDby name in a small inner block, never with WHEN OTHERS THEN NULL. - Use a default when a missing row is normal, and RAISE_APPLICATION_ERROR when it is a bug.
- Use BULK COLLECT or a cursor when zero or many rows are valid results.
- For "data exists" cases, check commits, schema, CHAR vs VARCHAR2 and time parts of dates.
- Remember that functions called from SQL turn NO_DATA_FOUND into NULL.