1

几天前发布了一个问题,并成功让我的触发器工作!但是遇到了一些新问题。

我有两张桌子:

CREATE TABLE  "ASSESSMENT" 
  (     "ASSESSMENT_NAME" VARCHAR2(50) NOT NULL ENABLE, 
        "DEADLINE_DATE" DATE NOT NULL ENABLE, 
         CONSTRAINT "ASSESSMENT_PK" PRIMARY KEY ("ASSESSMENT_NAME") ENABLE
  )

CREATE TABLE  "ASSESSMENT_ANNOUNCEMENT" 
  (    "ASSESSMENT_NAME" VARCHAR2(50) NOT NULL ENABLE, 
        "DEADLINE_DATE" DATE NOT NULL ENABLE, 
        "ATTENTION" VARCHAR2(500) NOT NULL ENABLE, 
        CONSTRAINT "ASSESSMENT_ANNOUNCEMENT_PK" PRIMARY KEY ("ASSESSMENT_NAME")  ENABLE
  )

我的触发器:

CREATE OR REPLACE TRIGGER  "TEST"
  AFTER INSERT OR UPDATE OR DELETE 
  ON ASSESSMENT
FOR EACH ROW
BEGIN
  IF :new.DEADLINE_DATE >= SYSDATE - 7
  THEN
    INSERT INTO ASSESSMENT_ANNOUNCEMENT(ASSESSMENT_NAME, DEADLINE_DATE ,ATTENTION)
      VALUES(:new.ASSESSMENT_NAME, :new.DEADLINE_DATE, 'DEADLINE IS 7 DAYS OR LESS!');
  END IF;
END;

插入在表格中正常工作。但是,当我更新ASSESSMENT表格时,表格中会插入一个新行ASSESSMENT_ANNOUNCEMENT- 它没有更新。

从表中删除会从ASSESSMENT表中删除行,ASSESSMENT但不会从表中删除条目ASSESSMENT_ANNOUNCEMENT

任何帮助和/或指导都会很棒!

4

2 回答 2

1

如果要更新或删除 中的行ASSESSMENT_ANNOUNCEMENT,则应使用updateordelete语句显式执行。
在触发器中使用以下结构:

IF INSERTING THEN
  -- actions for inserting
ELSIF UPDATING THEN
  -- actions for updating
ELSE
  -- actions for deleting
END IF;
于 2013-03-02T21:02:04.843 回答
0

只是为了给你一个完整的样本

CREATE OR REPLACE TRIGGER  "TEST"
    AFTER INSERT OR UPDATE OR DELETE 
    ON ASSESSMENT
FOR EACH ROW
BEGIN
    IF INSERTING THEN
        IF :new.DEADLINE_DATE >= SYSDATE - 7
        THEN
            INSERT INTO ASSESSMENT_ANNOUNCEMENT(ASSESSMENT_NAME, DEADLINE_DATE ,ATTENTION)
            VALUES(:new.ASSESSMENT_NAME, :new.DEADLINE_DATE, 'DEADLINE IS 7 DAYS OR LESS!');
        END IF;

    ELSIF UPDATING THEN
        UPDATE ASSESSMENT_ANNOUNCEMENT SET 
          ASSESSMENT_NAME=:new.ASSESSMENT_NAME,
          DEADLINE_DATE=:new.DEADLINE_DATE,
          ATTENTION='Deadline Updated'
        WHERE ASSESSMENT_NAME=:old.ASSESSMENT_NAME;

    ELSE
       DELETE ASSESSMENT_ANNOUNCEMENT
         WHERE ASSESSMENT_NAME=:old.ASSESSMENT_NAME;
    END IF; 
END;

根据你真实的业务逻辑和 PL/SQL 代码的大小,创建三个触发器可能会更清楚

CREATE OR REPLACE TRIGGER "TEST_AI_TRG" AFTER INSERT ON ASSESSMENT ...
CREATE OR REPLACE TRIGGER "TEST_AU_TRG" AFTER UPDATE ON ASSESSMENT ...
CREATE OR REPLACE TRIGGER "TEST_AD_TRG" AFTER DELETE ON ASSESSMENT ...
于 2013-03-03T07:12:35.327 回答