创建触发器,删除和更新其他表中的数据

时间:2013-03-14 16:05:25

标签: plsql triggers

我有两张桌子。我需要编写一个触发器IN PL / SQL,随时更新或删除表1中的PROJCODE(赋值),更新项目中的代码,并从项目中删除它在table1中不再出现的代码。然后打印出项目表中的列(代码)

我试着编写这段代码,但仍然得到代码没有声明的错误:new.code和:old.code是错误的绑定变量'OLD.CODE'!

CREATE OR REPLACE TRIGGER update_1
  BEFORE UPDATE OR DELETE OF PROJCODE
  ON ASSIGNMENT
  FOR EACH ROW
BEGIN
  IF UPDATING THEN
    UPDATE PRODUCT P
    SET P.CODE = :new.CODE
    WHERE P.CODE = :old.CODE;
  ELSE
     DELETE FROM PROJECT WHERE :old.CODE= CODE ;
  END IF;
     DBMS_OUTPUT.PUT_LINE(CODE);
END;

我怎样解决这个问题?

1 个答案:

答案 0 :(得分:0)

您的触发器应该变为:

CREATE OR REPLACE TRIGGER update_1
  BEFORE UPDATE OR DELETE ON PROJCODE
  REFERENCING new AS new old AS old
  FOR EACH ROW
BEGIN
  IF UPDATING THEN
    UPDATE PRODUCT P
    SET P.CODE = :new.CODE
    WHERE P.CODE = :old.CODE;
  ELSE
     DELETE FROM PROJECT WHERE :old.CODE= CODE ;
  END IF;
     DBMS_OUTPUT.PUT_LINE(CODE);
END;
/