Oracle- Variable without data

0

I have a problem and a question at the same time, I made a function to view billed requests and declare a function variable like this:

select filsaida
  into v_loja
  from mov_itped where pedido= p_pedido and status <> 6;
end ;

When you have a record that matches the WHERE clause, the function returns normal, but when it does not return a record, it gives the error ORA-01403: data not found.

    
asked by anonymous 28.06.2018 / 20:07

1 answer

3

In an "into" condition you must return only one record. If multiple returns will give the ORA-01422 exception, and if none returns it will give the exception you cited.

If you have a default value, for your variable when the record does not exist, you can use an exception block by handling the NO_DATA_FOUND exception. Ex:

Declare
  numero integer;
begin
  begin
    select 1 into numero 
      from dual 
     where 1 = 2;
  exception
    when NO_DATA_FOUND then
      numero := 0;
  end;
  dbms_output.put_line(numero);
end;
    
28.06.2018 / 20:19