Solved

How to handle NO DATA EXCEPTION error

Posted on 2013-05-15
7
346 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 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 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to take different types of Oracle backups using RMAN.

695 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