几天前发布了一个问题并成功让我的触发器工作! 但是有一些新问题。
我有两张桌子:
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
表。
任何帮助和/或指导都会很棒!
答案 0 :(得分:1)
如果您要更新或删除ASSESSMENT_ANNOUNCEMENT
中的行,则应使用update
或delete
语句明确地执行此操作。
在触发器中使用以下结构:
IF INSERTING THEN
-- actions for inserting
ELSIF UPDATING THEN
-- actions for updating
ELSE
-- actions for deleting
END IF;
答案 1 :(得分: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 ...