Lets say we have this TableA:
| EMPNO | EXPCODE | EXPYEARS |
|---|---|---|
| 1 | EXP1 | 4 |
| 1 | EXP4 | 4 |
The premise of this trigger is that whenever someone makes an update to Table A, EXP4 is not allowed to be less than EXP1.
My first iteration was something along the lines of:
BEFORE INSERT OR UPDATE ON TableA
FOR EACH ROW
DECLARE
EXP NUMBER(7,2);
BEGIN
IF (:NEW.EXPCODE = 'EXP1') THEN
SELECT NVL(X.EXPYEARS,0) INTO EXP FROM TableA X WHERE X.EMPNO = :NEW.EMPNO AND X.EXPCODE = 'EXP4';
IF (EXP < :NEW.EXPYEARS) THEN raise_application_error(-20010,'EXP4 CANNOT BE LESS THAN EXP1'); END IF;
ELSIF (:NEW.EXPCODE = 'EXP4') THEN
SELECT NVL(X.EXPYEARS,0) INTO EXP FROM TableA X WHERE X.EMPNO = :NEW.EMPNO AND X.EXPCODE = 'EXP1';
IF (EXP > :NEW.EXPYEARS) THEN raise_application_error(-20010,'EXP4 CANNOT BE LESS THAN EXP1'); END IF;
END IF;
END;
I've gone through several iterations after that to try and get around this issue but to no avail. Any help would be appreciated. Our company doesn't own the application using the database so I'm trying to make QoL changes on the database side.