Skip to Main Content
  • Questions
  • Not able to store no_data_found exception in logging table

Breadcrumb

Question and Answer

Chris Saxon

Thanks for the question, Ankit.

Asked: July 11, 2017 - 3:32 am UTC

Last updated: July 11, 2017 - 9:37 am UTC

Version: 12.1.0

Viewed 1000+ times

You Asked

Hi,

I have one stored procedure when I am passing one input parameter. With that input parameter, if there is no_data_found then I am raising the exception trying to store no_data_found error in that error logs table but I am not able to do it. Please find my code below,

Stored Procedure:

create or replace procedure dumm_proc (p_opportunity_number cct_opportunity.opportunity_number%type)
as

v_oppo_id varchar2(50);
l_message varchar2(50) := sqlerrm;
l_code varchar2(50) := sqlcode;

begin

select opportunity_id into v_oppo_id 
from cct_opportunity
where opportunity_number =  p_opportunity_number;


exception
when no_data_found then
xxcct_error_logs(cct_error_seq.nextval,null,v_oppo_id,null,null,l_message,l_code,'dumm_proc',null,sysdate,user,sysdate,user);
raise;

when others then
xxcct_error_logs(cct_error_seq.nextval,null,v_oppo_id,null,null,l_message,l_code,'dumm_proc',null,sysdate,user,sysdate,user);
raise;
end;


Storing the values into the table using another error log procedure (xxcct_error_logs) but always it stores with normal, successful completion.

Please suggest how can I store no_data_found error by calling error_logs procedure.

Thaks.

and Chris said...

You assign the values to l_code and l_message at the start of the procedure. When everything's good. So when you reference these in the exception handler, they still have these values!

To get the error information, you need to call sqlerrm and sqlcode in the exception block itself:

declare

  l_message varchar2(50) := sqlerrm;
  l_code varchar2(50) := sqlcode;

begin
  dbms_output.put_line(l_code || ' ' || l_message);
  raise no_data_found;

exception
  when no_data_found then
    dbms_output.put_line(l_code || ' ' || l_message);
    dbms_output.put_line(sqlcode || ' ' || sqlerrm);
end;
/

0 ORA-0000: normal, successful completion
0 ORA-0000: normal, successful completion
100 ORA-01403: no data found

Rating

  (1 rating)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Ankit Bhatt, July 11, 2017 - 1:01 pm UTC

Thanks a lot!!!

More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library