Search notes:

Oracle: Errors ORA-01403 and ORA-01422

Test table

Create a test table and fill it with some values.
Note: no record with id = 4, two records with id = 3:
create table tq84_test (
   id    number,
   val   varchar2(10)
);

begin
   insert into tq84_test values (1, 'one'  );
   insert into tq84_test values (2, 'two'  );
   insert into tq84_test values (3, 'three');
   insert into tq84_test values (3, 'THREE');
   insert into tq84_test values (5, 'five' );
   commit;
end;
/

A procedure and function selecting from the table

The following function takes a number and returns the corresponding value from the test table:
create or replace function tq84_fnc(id number)
   return varchar2
   authid definer
as
   ret varchar2(10);   
begin
   select val into ret
   from   tq84_test t
   where  t.id =  tq84_fnc.id;
   
   return ret;
end tq84_fnc;
/
This procedure does essentially the same, but does print the value rather then returning it:
create or replace procedure tq84_prc(id number)
   authid definer
as
   ret varchar2(10);   
begin
   select val into ret
   from   tq84_test t
   where  t.id =  tq84_prc.id;
   
   dbms_output.put_line(ret);
end tq84_prc;
/

«Normal» operation

Prints two
begin
   dbms_output.put_line(tq84_fnc(2));
end;
/
Also prints two:
begin
   tq84_prc(2);
end;
/
Selects two:
select tq84_fnc(2) from dual;

Multiple values

Following PL/SQL block throws ORA-01422: exact fetch returns more than requested number of rows:
begin
   dbms_output.put_line(tq84_fnc(3));
end;
/
So does this one:
begin
   tq84_prc(3);
end;
/
And also this select statement:
select tq84_fnc(3) from dual;

Non existing value

The following PL/SQL block throws ORA-01403: no data found:
begin
   dbms_output.put_line(tq84_fnc(4));
end;
/
So does the next:
begin
   tq84_prc(4);
end;
/
Possibly surprisingly, the following select statement returns null rather then throwing an ORA-01403:
select tq84_fnc(4) from dual;

See also

Other Oracle error messages

Index