views:

42

answers:

1

Hi, I have a small doubt regarding BEFORE INSERT TRIGGER in oracle, my trigger looks like this:

CREATE OR REPLACE TRIGGER some_trigger BEFORE INSERT
ON some_table   REFERENCING NEW AS newRow  
FOR EACH ROW
DECLARE
some_var          number(25, 4);

BEGIN
-- do some stuff
    :newRow.some_column  :=some_var;

 exception
      when no_data_found then
  NULL;
    when others then
    NULL;
END;

Here the update which I am doing on newRow.some_column is an optional thing, so my requirement is that even the trigger fails, the newRow should be inserted into the table and this is why I am eating up exceptions.

Is my assumption correct that if I eat up exception, the newRow will be inserted into the table in all scenarios ?

Thanks heaps.

+3  A: 

Your exception "handling" will make sure that the insert succeeds, even if you have an exception in your trigger.

Some thoughts:

  1. Your current code cannot cause a NO_DATA_FOUND-exception.

  2. Do you really want your code to fail silently?

  3. Why do you catch both NO_DATA_FOUND and OTHERS and ignore both? OTHERS will catch NO_DATA_FOUND too.

EDIT

I'd just catch the NO_DATA_FOUND and add a good comment about why you can silently ignore it in your case.
Make sure that your SELECT only returns a single row, otherwise TOO_MANY_ROWS needs to be handled too.

Ignoring OTHERS is generally considered bad practice. Your code could fail and you'd never notice. There is a new Compiler Warning for this, actually.

Peter Lang
+1 Exception "mishandling" would be more accurate!
Tony Andrews
Thanks peter, 1. the commented part "-- do some stuff" fires a select query which may cause a NO_DATA_FOUND. 2. Yes, I want to fail silently as this is an optional update. 3. I will catch and gobble only "OTHERS". thanks again.
Ravi Gupta
OK, now I got a hold of it, thank you for helping me out.
Ravi Gupta