[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 351
  • Last Modified:

How to handle NO DATA EXCEPTION error

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
cookiejar
Asked:
cookiejar
  • 3
  • 2
  • 2
1 Solution
 
Naveen KumarProduction Manager / Application Support ManagerCommented:
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
 
Naveen KumarProduction Manager / Application Support ManagerCommented:
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
 
cookiejarAuthor Commented:
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
Independent Software Vendors: 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!

 
Naveen KumarProduction Manager / Application Support ManagerCommented:
i could not understand your question now. can you give your full code to unerstand ?
0
 
slightwv (䄆 Netminder) Commented:
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
 
cookiejarAuthor Commented:
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
 
slightwv (䄆 Netminder) Commented:
>>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

Independent Software Vendors: 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!

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now