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.
Your exception “handling” will make sure that the insert succeeds, even if you have an exception in your trigger.
Some thoughts:
Your current code cannot cause a
NO_DATA_FOUND-exception.Do you really want your code to fail silently?
Why do you catch both
NO_DATA_FOUNDandOTHERSand ignore both?OTHERSwill catchNO_DATA_FOUNDtoo.EDIT
I’d just catch the
NO_DATA_FOUNDand 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_ROWSneeds to be handled too.Ignoring
OTHERSis generally considered bad practice. Your code could fail and you’d never notice. There is a new Compiler Warning for this, actually.