Solved

How to handle NO DATA EXCEPTION error

Posted on 2013-05-15
7
340 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
  • 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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 76

Accepted Solution

by:
slightwv (䄆 Netminder) earned 500 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 76

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

This article started out as an Experts-Exchange question, which then grew into a quick tip to go along with an IOUG presentation for the Collaborate confernce and then later grew again into a full blown article with expanded functionality and legacy…
Note: this article covers simple compression. Oracle introduced in version 11g release 2 a new feature called Advanced Compression which is not covered here. General principle of Oracle compression Oracle compression is a way of reducing the d…
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.
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…

747 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now