2

I've got an insert/update trigger set up which prevents an employee from existing in two tables at the same time. It works fine with catching illegal insertions/updates but i'm also getting another error report when testing the trigger with illegal insertions/updates.

Here's my code:

CREATE OR REPLACE TRIGGER check_foobar
BEFORE INSERT OR UPDATE OF VarX ON FOOBAR
FOR EACH ROW
DECLARE
    counter NUMBER(38);
BEGIN  
    SELECT count(*)
    INTO counter
    FROM BARFOO
    WHERE VarX = :NEW.VarX
    GROUP BY VarX;

IF counter > 0 THEN 
    RAISE_APPLICATION_ERROR(-20001, 'This is an illegal insertion/update');
    END IF;

EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('TEST');

END;
/

And the errors ORA-06512 and ORA-04088 i'm not sure of:

SQL> INSERT INTO DRIVER VALUES(2,  10345, 'AVAILABLE');

Error starting at line : 26 File @test.sql
In command -
INSERT INTO FOOBAR VALUES(2)
Error report -
ORA-20001: This is an illegal insertion/update
ORA-06512: at "HR.CHECK_FOOBAR", line 11
ORA-04088: error during execution of trigger 'HR.CHECK_FOOBAR'

When i add an exception handler for the select statement my trigger stops working properly and the illegal insertion isn't prevented. But the execution error is prevented.

UPDATE: I've added a group by and an exception to the trigger, so now the trigger still works with an exception handler but the errors ORA-06512 and ORA-04088 are still coming up with the illegal insertion/update.

Line 11 mentioned in the error is

GROUP BY VarX;

Any advice would be much appreciated.

2 Answers 2

3

There is no problem here, it is working as expected.

Error starting at line : 26 File @test.sql
In command -
INSERT INTO FOOBAR VALUES(2)

This is referring to the line in your script where you have the INSERT statement. It's telling you an exception was raised at this point.

Error report -

This is showing the error stack:

ORA-20001: This is an illegal insertion/update

This is the actual exception that was raised.

ORA-06512: at "HR.CHECK_FOOBAR", line 11
ORA-04088: error during execution of trigger 'HR.CHECK_FOOBAR'

These are additional messages just to tell you where the exception was originally raised, in this case, in your trigger on line 11, where the RAISE_APPLICATION_ERROR is, as you might expect. Note that line numbers for triggers refer to the executable portion of the trigger, so in your case the DECLARE is line 1.

Sign up to request clarification or add additional context in comments.

Comments

-1

The ORA-04088 error means that the trigger has an un-handled exception. You are raising a application error but then not handling it. You need to handle this exception as per below.

CREATE OR REPLACE TRIGGER check_foobar
BEFORE INSERT OR UPDATE OF VarX ON FOOBAR
FOR EACH ROW
DECLARE
    counter NUMBER(38);
    BEGIN  
       SELECT count(*)
       INTO counter
       FROM BARFOO
       WHERE VarX = :NEW.VarX
       GROUP BY VarX;

    IF counter > 0 THEN 
       RAISE_APPLICATION_ERROR(-20001, 'This is an illegal insertion/update');
    END IF;

EXCEPTION 
   WHEN NO_DATA_FOUND THEN 
      DBMS_OUTPUT.PUT_LINE('TEST');
   WHEN OTHERS THEN
      IF SQLCODE = -20001 THEN
        -- do some logging
        RAISE;
      ELSE
        -- do some logging and any other actions you feel are needed.   Then depending on needs you can raise or not.

      END IF;
END;
/

2 Comments

It would be pointless to handle this exception; it would undo the purpose of the exception which is to stop the insert or update. Also, you can't have two EXCEPTION sections in the same block.
Sorry that was a typo, fixed the issue, so that it is not two exception sections just multiple catches. catching the exception and explicitly raising it should stop the un handled exception appearing as you are handling and raising it but will still stop the record being inserted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.