如果删除的行具有未超过一年的datefin字段,我创建了一个触发器来引发错误

dan*_*iel 0 oracle plsql database-trigger

这是我的代码:

create or replace trigger tr_interdit
before delete on reservation
for each row
DECLARE
    V_res_date RESERVATION.DATEFIN%type;
begin
    SELECT DATEFIN into V_res_date
    FROM reservation
    where DATEFIN = :old.DATEFIN;

    if V_res_date<add_months(V_res_date,-12)
        then RAISE_APPLICATION_ERROR(-20001, 'Date fin na pas depasse un ans');
    end if;

end tr_interdit;
/
Run Code Online (Sandbox Code Playgroud)

但是当我删除一行后,即使它不应该被删除,我也会收到这个错误.

这是错误:

删除"DANIEL"."保留",其中ROWID ='AAAFCvAABAAALHhAAA'和ORA_ROWSCN ='3392006'和("NUMR"为空或"NUMR"不为空)ORA-04091:表DANIEL.RESERVATION正在变异,触发/功能可能看不到它ORA-06512:在"DANIEL.TR_INTERDIT",第4行ORA-04088:执行触发器'DANIEL.TR_INTERDIT'时出错

a_h*_*ame 5

无需SELECT,只需直接访问旧值即可.

你的病情v_res_date < add_months(v_res_date,-12)永远不会成真.

我假设你不想v_res_dateadd_months()通话中使用,但要比较:old.DATEFIN"当前日期"的值 -sysdate

create or replace trigger tr_interdit
before delete on reservation
for each row
begin
    if :old.DATEFIN < add_months(sysdate,-12)
        then RAISE_APPLICATION_ERROR(-20001, 'Date fin na pas depasse un ans');
    end if;

end tr_interdit;
/
Run Code Online (Sandbox Code Playgroud)