我正在创建一个触发器,当在表TEACHING_INFORMATION上完成插入或更新时,该触发器起作用,基本上我只是输入输入的年份字段并确保同一个人没有相同的课程去年如果他们不允许的话。我使用:NEW值将现有数据与输入数据进行比较,但是我收到错误(放在下面)。这是我所做的代码:
CREATE OR REPLACE TRIGGER TRIGGER1
AFTER INSERT OR UPDATE OF YEAR ON TEACHING_INFORMATION
DECLARE requestedyear TEACHING_INFORMATION.YEAR%type;
pragma autonomous_transaction;
BEGIN
SELECT year INTO requestedyear FROM TEACHING_INFORMATION WHERE staffID = :NEW.staffID AND COURSEID = :NEW.courseID;
IF (:NEW.year = requestedyear + 1) THEN
RAISE_APPLICATION_ERROR(-20250, 'Cannot teach the same class for 2 consecutive years');
END IF;
END;
错误:
ORA-04082: NEW or OLD references not allowed in table level triggers
我试图将FOR EACH ROW
条件添加到触发器但是这不起作用,因为发生了另一个错误,表明它正在查看太多行,因为我真的只需要查看一个。我的逻辑可能就此而言,因为我是触发器的新手,但是在正确的方向上任何帮助/指导都会受到赞赏。
答案 0 :(得分:0)
:NEW和:OLD只能在行级别触发器中使用(FOR EACH ROW选项),因此oracle不知道应该采用哪个:NEW。尝试添加:引用new作为每行旧旧。所以你的代码应该是这样的:
CREATE OR REPLACE TRIGGER TRIGGER1
AFTER INSERT OR UPDATE OF YEAR ON TEACHING_INFORMATION
referencing new as new old as old for each row
DECLARE requestedyear TEACHING_INFORMATION.YEAR%type;
pragma autonomous_transaction;
BEGIN
SELECT year INTO requestedyear FROM TEACHING_INFORMATION WHERE staffID = :NEW.staffID AND COURSEID = :NEW.courseID;
IF (:NEW.year = requestedyear + 1) THEN
RAISE_APPLICATION_ERROR(-20250, 'Cannot teach the same class for 2 consecutive years');
END IF;
END;