0

I have a question about my trigger that I'm trying to create between two tables. When one table is updated the other should be updated too, but I seem to be missing proper syntax.

CREATE OR REPLACE TRIGGER TRIG_DEPT_ONUPDATE
AFTER UPDATE OF DEPT_ID ON DEPARTMENT FOR EACH ROW
BEGIN
    UPDATE TEAM
        SET DEPT_ID = :NEW.DEPT_ID
        WHERE TEAM.DEPT_ID = :NEW.DEPT_ID;
END;
/

I get errors on update ("integrity constraint (%s.%s) violated - child record found"), but using the code:

CREATE OR REPLACE TRIGGER TRIG_DEPT_ONUPDATE
AFTER UPDATE OF DEPT_ID ON DEPARTMENT FOR EACH ROW
BEGIN
    UPDATE TEAM
        SET DEPT_ID = :NEW.DEPT_ID;
END;
/

it changes every single row after an update, though only select few need the change. Should an If statement be worked in somehow?

1
  • There is no Oracle 4.0.0.13 Commented Mar 6, 2014 at 16:18

2 Answers 2

1

To access the newly updated row values, you need a row level trigger not a statement level trigger:

CREATE OR REPLACE TRIGGER TRIG_DEPT_ONUPDATE
AFTER UPDATE OF DEPT_ID ON TEAM
for each row
BEGIN
    UPDATE DEPARTMENT
    SET DEPT_ID = :NEW.DEPT_ID
    Where DEPT_ID = :OLD.DEPT_ID;
END;
Sign up to request clarification or add additional context in comments.

2 Comments

Oh, that changed all of my rows, but not the one needed, when using 'For Each Row'.
Thanks for the help, I still got the ("integrity constraint (%s.%s) violated - child record found") upon update, but then I just updated the other table and it worked out fine!
0

I guess this row

DEPT_ID = DEPT_ID - :NEW.DEPT_ID

generates some DEPT_ID which is not existing. That's the error reason.

Comments

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.