无论if语句值如何,此触发器最终都会引发错误。我基本上是在同一张桌子上选择正在进行删除,而且不喜欢它。
CREATE OR REPLACE TRIGGER delete_schedules
AFTER DELETE
ON SCHEDULES
FOR EACH ROW
DECLARE
lParentCond schedules.cond_code%type;
lParentActive schedules.active_flag%type;
lError exception;
BEGIN
if :OLD.thread is not null then
select cond_code, active_flag
into lParentCond, lParentActive
from schedules where schedule_seq = :old.thread;
if lParentCond = 'OK' and lParentActive in ('*', 'F') then
raise lError;
end if;
end if;
EXCEPTION
when lError then
raise;
WHEN OTHERS THEN RAISE;
END delete_schedules;
任何解决方法的想法?
答案 0 :(得分:2)
你可能会遇到可怕的'MUTATING TABLE'错误。 Oracle不允许我们从AFTER触发器中定义触发器的表中获取数据 - 但在这种情况下,您不需要,因为“旧”值已经可用。将您的触发器重写为:
CREATE OR REPLACE TRIGGER delete_schedules
AFTER DELETE
ON SCHEDULES
FOR EACH ROW
BEGIN
if :OLD.thread is not null AND
:OLD.COND_CODE = 'OK' and
:OLD.ACTIVE_FLAG in ('*', 'F')
then
RAISE_APPLICATION_ERROR(-20100, 'Invalid combination of COND_CODE and ACTIVE_FLAG');
end if;
END delete_schedules;
这假定(基于在问题中使用单个SELECT),在给定的THREAD值的SCHEDULES中只有一行。如果不是这种情况,还有其他解决方法,包括使用COMPOUND TRIGGER。
祝你好运。