04098. "trigger '%s.%s' is invalid and failed re-validation"

Gregy

I would like to write a trigger which blocks deleting records from table and blocks decreasing salary on update. It works well as two seperate triggers, but I would like to merge it into one.

Here is my trigger after merging:

  CREATE OR REPLACE TRIGGER test
    BEFORE DELETE OR UPDATE
    ON emp
    FOR EACH ROW
    BEGIN
      IF UPDATING THEN
        IF :NEW.sal < :OLD.sal THEN
          raise_application_error(-20500, 'You cannot decrease emp salary');
        END IF;
      ELSE DELETING THEN
        raise_application_error(-20500, 'You cannot delete records from emp');
      END IF;
    END;

The problem is when I try to update or delete record I get an error:

04098. 00000 -  "trigger '%s.%s' is invalid and failed re-validation"
*Cause:    A trigger was attempted to be retrieved for execution and was
           found to be invalid.  This also means that compilation/authorization
           failed for the trigger

I think that there is a problem around this line - BEFORE DELETE OR UPDATE because as I know if I FOR EACH ROW statment I should write is as BEFORE UPDATE on sal, but then I don't know how to join BEFORE DELETE statment into this.

EDIT:
Problem was around ELSE statment. It should be changed to ELSIF

Bob Jarvis - Reinstate Monica

Your IF statement syntax is incorrect. It should be:

CREATE OR REPLACE TRIGGER test
  BEFORE DELETE OR UPDATE
  ON emp
  FOR EACH ROW
BEGIN
  IF UPDATING AND :NEW.sal < :OLD.sal THEN
    raise_application_error(-20500, 'You cannot decrease emp salary');
  ELSIF DELETING THEN
    raise_application_error(-20500, 'You cannot delete records from emp');
  END IF;
END test;

I also combined the two IF statements used in the UPDATING case into one to keep it simpler.

EDIT

In the case where you don't want to allow salaries to be decreased another possible solution is to quietly change the salary back to its original value, as in:

CREATE OR REPLACE TRIGGER test
  BEFORE DELETE OR UPDATE
  ON emp
  FOR EACH ROW
BEGIN
  IF UPDATING AND :NEW.sal < :OLD.sal THEN
    :NEW.sal := :OLD.sal;  -- restore original salary
  ELSIF DELETING THEN
    raise_application_error(-20500, 'You cannot delete records from emp');
  END IF;
END test;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Oracle Trigger ORA-04098: trigger is invalid and failed re-validation

From Dev

04098. "trigger '%s.%s' is invalid and failed re-validation"

From Dev

Merging Users in Kinvey

From Dev

PostgreSQL: Efficiently split JSON array into rows

From Dev

boost::asio how to read full buffer in right way?

From Dev

GIT SVN: fetching a recreated SVN branch without the wrong merge parent

From Dev

How to find out value of a particular attribute index in core data model iOS

From Dev

iOS - iPad preview in Xcode is missing

From Dev

Java: Method hooking & Finding object instances

From Dev

Using git diff, how can I show the patch from the index to a given commit?

From Dev

Cast for upcasting only

From Dev

How can I multiply or divide value under the cursor by a repeat count?

From Dev

Can`t compile boost spirit word_count_lexer example

From Java

How to make a for loop variable const with the exception of the increment statement?

From Java

Count letters in a text in the Welsh language

From Java

Why does using the ternary operator to return a string generate considerably different code from returning in an equivalent if/else block?

From Java

No Firebase App '[DEFAULT]' has been created - call Firebase.initializeApp() in Flutter and Firebase

From Java

Changes using mutable reference of a field are not reflected after move of the original instance

From Java

Elastic Search indexes gets deleted frequently

From Java

Forgot do in do... while loop

From Java

How to fix "An unexpected error occurred. Please try again later. (7100000)" error in Google Play Console?

From Java

Does C# perform short circuit evaluation of if statements with await?

From Java

Why doesn't Python give any error when quotes around a string do not match?

From Java

Why is this regular expression so slow in Java?

From Java

Why are my two tuples containing strings, created the same way, not equal?

From Java

How to pass array initialization thru custom structure?

From Java

Why does Python code run faster in a function?

From Java

How to set same alignment for these two text

From Java

How to wrap title on to new line react native

Related Related

  1. 1

    Oracle Trigger ORA-04098: trigger is invalid and failed re-validation

  2. 2

    04098. "trigger '%s.%s' is invalid and failed re-validation"

  3. 3

    Merging Users in Kinvey

  4. 4

    PostgreSQL: Efficiently split JSON array into rows

  5. 5

    boost::asio how to read full buffer in right way?

  6. 6

    GIT SVN: fetching a recreated SVN branch without the wrong merge parent

  7. 7

    How to find out value of a particular attribute index in core data model iOS

  8. 8

    iOS - iPad preview in Xcode is missing

  9. 9

    Java: Method hooking & Finding object instances

  10. 10

    Using git diff, how can I show the patch from the index to a given commit?

  11. 11

    Cast for upcasting only

  12. 12

    How can I multiply or divide value under the cursor by a repeat count?

  13. 13

    Can`t compile boost spirit word_count_lexer example

  14. 14

    How to make a for loop variable const with the exception of the increment statement?

  15. 15

    Count letters in a text in the Welsh language

  16. 16

    Why does using the ternary operator to return a string generate considerably different code from returning in an equivalent if/else block?

  17. 17

    No Firebase App '[DEFAULT]' has been created - call Firebase.initializeApp() in Flutter and Firebase

  18. 18

    Changes using mutable reference of a field are not reflected after move of the original instance

  19. 19

    Elastic Search indexes gets deleted frequently

  20. 20

    Forgot do in do... while loop

  21. 21

    How to fix "An unexpected error occurred. Please try again later. (7100000)" error in Google Play Console?

  22. 22

    Does C# perform short circuit evaluation of if statements with await?

  23. 23

    Why doesn't Python give any error when quotes around a string do not match?

  24. 24

    Why is this regular expression so slow in Java?

  25. 25

    Why are my two tuples containing strings, created the same way, not equal?

  26. 26

    How to pass array initialization thru custom structure?

  27. 27

    Why does Python code run faster in a function?

  28. 28

    How to set same alignment for these two text

  29. 29

    How to wrap title on to new line react native

HotTag

Archive