?
Solved

How to handle NO DATA EXCEPTION error

Posted on 2013-05-15
7
Medium Priority
?
348 Views
Last Modified: 2013-06-07
When no error occurs, the code following the exception statement does not execute.  Where an error does occurs, it does execute.
What am I doing wrong?

BEGIN

SELECT value INTO sysvall FROM SYS_VALS WHERE NAME ='TEST'  
EXCEPTION
       WHEN no_data_found then
         v_sysval := 'NO ENTRY';

   for loop
       ....
   end loop;

END;
0
Comment
Question by:cookiejar
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 28

Expert Comment

by:Naveen Kumar
ID: 39170094
exception handling section is for the while begin - end block. so it has to be as shown below :

BEGIN

SELECT value INTO sysvall FROM SYS_VALS WHERE NAME ='TEST'   ;

   for loop
       ....
   end loop;

EXCEPTION
       WHEN no_data_found then
         v_sysval := 'NO ENTRY';
    when others then
         v_sysval := 'in other';


END;
0
 
LVL 28

Expert Comment

by:Naveen Kumar
ID: 39170108
also i suggest to add few debug/print messages when you are testing the code which can be removed later when you do not need.

BEGIN

SELECT value INTO sysvall FROM SYS_VALS WHERE NAME ='TEST'   ;

   for loop
       ....
   end loop;

EXCEPTION
       WHEN no_data_found then
         v_sysval := 'NO ENTRY';
  dbms_output.put_line('in no data found exception');
   dbms_output.put_line('err code:' || sqlcode );
   dbms_output.put_line('error message :' || sqlerrm );

    when others then
         v_sysval := 'in other';
  dbms_output.put_line('in no data found exception');
   dbms_output.put_line('err code:' || sqlcode );
   dbms_output.put_line('error message :' || sqlerrm );


END;

Refer to teh below links for more information :

http://psoug.org/reference/exception_handling.html
0
 

Author Comment

by:cookiejar
ID: 39170123
I am trying to understand the order of processing.   I am getting the sysval variable to determine what value I should insert into a column in a table.  If 'no entry', flag column with the value 'NO ENTRY'.

Should I use Select Count(1) to determine if I should do
the select into variable?  I prefer not to.


For Loop
Loop
   
   If v_sysval = 'NO ENTRY'  Then
      v_which_value  := 1
else if v_sysval =  'SomeValue'  Then
     v_which_value  := 2
else
     v_which_value  :=  3
end if;

 The v_which_value is inserted into a column in the database
 insert table
           col1,
           col2,
           col3,
           v_which_value
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 28

Expert Comment

by:Naveen Kumar
ID: 39170142
i could not understand your question now. can you give your full code to unerstand ?
0
 
LVL 77

Accepted Solution

by:
slightwv (䄆 Netminder) earned 2000 total points
ID: 39171130
Try a nested pl/sql block.

Each block can have it's own exception handler.

BEGIN

   begin
          SELECT value INTO sysvall FROM SYS_VALS WHERE NAME ='TEST'   ;
 
          EXCEPTION
              WHEN no_data_found then
                     v_sysval := 'NO ENTRY';
             when others then
                    v_sysval := 'in other';
    end;



   for loop
       ....
   end loop;



END; 

Open in new window

0
 

Author Comment

by:cookiejar
ID: 39173392
My stored procedure is constructed like such:
I encountered a constraint error in one of the blocks.
I would like all transactions rollback if an error occurs, but instead only the block where the error occurred.  Should I not commit  within the blocks or should I commit at the end of all of the inserts?
BEGIN

 begin
  insert
  select
   commit;
 end;

 begin
  insert
  select
   commit;
 end;


 begin
  insert
  select
   commit;
 end;


EXCEPTION -- Add more error handling logic
    WHEN OTHERS THEN              
      ROLLBACK;
      RAISE;



END;
/
0
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 39173428
>>Should I not commit  within the blocks or should I commit at the end of all of the inserts?

Given that layout a rollback is only as good as the last commit.

If you have enough UNDO then forget about the commit's in each block.   One VERY large transaction has it's own set of issues you need to worry about.

Only you know your requirements and what is 'best' for what you are trying to do.

You might look into using staging tables (with necessary constraints in place) where you can insert into them first to make sure everything checks out.  Then perform inserts from them into your main tables if it is all OK.

Depending on your specific requirements, these might be global temporary tables or even normal tables with nologging to avoid the UNDO.

I also suggest you look into deferred constraints.  These are not validated until commit:
http://docs.oracle.com/cd/E11882_01/server.112/e25789/datainte.htm#CNCPT88892
and
http://docs.oracle.com/cd/E11882_01/server.112/e25494/general005.htm#ADMIN11550
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why doesn't the Oracle optimizer use my index? Querying too much data Most Oracle developers know that an index is useful when you can use it to restrict your result set to a small number of the total rows in a table. So, the obvious side…
From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…
This video shows how to recover a database from a user managed backup
Suggested Courses

741 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question