简体   繁体   中英

Oracle trigger syntax

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?

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;

I guess this row

DEPT_ID = DEPT_ID - :NEW.DEPT_ID

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM