Solved

How to handle NO DATA EXCEPTION error

Posted on 2013-05-15
7
344 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
Configuring and using Oracle Database Gateway for ODBC Introduction First, a brief summary of what a Database Gateway is.  A Gateway is a set of driver agents and configurations that allow an Oracle database to communicate with other platforms…
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

792 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