我如何才能使用else if,然后将值放入触发器的表中?

时间:2019-04-27 23:42:37

标签: sql oracle plsql database-trigger

我有两个表:projectpostponedproject 更新了duedate中的project之后,我创建了三个触发器:change_project_duedatepostponedproject_statuspostponedproject_difference

第一个是将旧的和更新的duedate和项目表主键prono移到postponedproject表中:

create or replace trigger change_project_duedate
after update of duedate on project
for each row
begin
insert into postponedproject (Prono, oldduedate, newduedate, status, difference, reason)
values (:old.prono, :old.duedate, :new.duedate, null, null, null);
end;

第二个是查看newduedate是否晚于旧oldduedate,然后将值放入postponedproject.status

create or replace trigger postponedproject_status
before insert on postponedproject
for each row
declare
differencestatement postponedproject.status%type;
begin
if :new.newduedate > :new.oldduedate then
    differencestatement := 'Project has been delayed';
Else 
    differencestatement := 'Project has been scheduled to finish eariler';
end if;
    insert into postponedproject (status)
    values (differencestatement);
end;

第三个是在difference表中的oldduedatenewduedate之间放置值postponedproject

create or replace trigger postponedproject_difference
before insert on postponedproject
for each row
begin
:new.difference := :new.newduedate - :new.oldduedate;
end;

但是,当我更新项目表中的duedate时,它给了我这样的错误:

Error report -
ORA-00036: maximum number of recursive SQL levels (50) exceeded
ORA-06512: at "21363937.POSTPONEDPROJECT_STATUS", line 9
ORA-04088: error during execution of trigger '21363937.POSTPONEDPROJECT_STATUS'
ORA-06512: at "21363937.POSTPONEDPROJECT_STATUS", line 9
ORA-04088: error during execution of trigger '21363937.POSTPONEDPROJECT_STATUS'
ORA-06512: at "21363937.POSTPONEDPROJECT_STATUS", line 9
ORA-04088: error during execution of trigger '21363937.POSTPONEDPROJECT_STATUS'
ORA-06512: at "21363937.POSTPONEDPROJECT_STATUS", line 9
ORA-04088: error during execution of trigger '21363937.POSTPONEDPROJECT_STATUS'
ORA-06512: at "21363937.POSTPONEDPROJECT_STATUS", line 9
ORA-04088: error during execution of trigger '21363937.POSTPONEDPROJECT_STATUS'
ORA-06512: at "21363937.POSTPONEDPROJECT_STATUS", line 9
ORA-04088: error during execution of trigger '21363937.POSTPONEDPROJECT_STATUS'
ORA-06512: at "21363937.POSTPONEDPROJECT_STATUS", line 9
ORA-04088: error during execution of trigger '21363937.POSTPONEDPROJECT_STATUS'
ORA-06512: at "21363937.POSTPONEDPROJECT_STATUS", line 9
ORA-04088: error during execution of trigger '21363937.POSTPONEDPROJECT_STATUS'
ORA-06512: at "21363937.POSTPONEDPROJECT_STATUS", line 9
ORA-04088: error during execution of trigger '21363937.POSTPONEDPROJECT_STATUS'
ORA-06512: at "21363937.POSTPONEDPROJECT_STATUS", line 9
ORA-04088: error during execution of trigger '21363937.POSTPONEDPROJECT_STATUS'
ORA-06512: at "21363937.POSTPONEDPROJECT_STATUS", line 9
ORA-04088: error during execution of trigger '21363937.POSTPONEDPROJECT_STATUS'
ORA-06512: at "21363937.POSTPONEDPROJECT_STATUS", line 9
ORA-04088: error during execution of trigger '21363937.POSTPONEDPROJECT_STATUS'
ORA-06512: at "21363937.POSTPONEDPROJECT_STATUS", line 9
ORA-04088: error during execution of trigger '21363937.POSTPONEDPROJECT_STATUS'
ORA-06512: at "21363937.POSTPONEDPROJECT_STATUS", line 9
ORA-04088: error during execution of trigger '21363937.POSTPONEDPROJECT_STATUS'
ORA-06512: at "21363937.POSTPONEDPROJECT_STATUS", line 9
ORA-04

如果有人可以帮我,我真的很感激。非常感谢

1 个答案:

答案 0 :(得分:1)

您在表postponedproject_status上的触发器postponedproject在表postponedproject上执行插入。这将导致触发器触发。它将在postponedproject中插入一条记录。这将导致触发器触发。直到您达到五十级递归为止。

除了执行INSERT之外,您只需要对:NEW命名空间进行分配即可。

此外,您不需要两个触发器。舍弃postponedproject_difference并将任务分配到另一个触发器中:

create or replace trigger postponedproject_status
before insert on postponedproject
for each row
declare
    differencestatement postponedproject.status%type;
begin

    :new.difference := :new.newduedate - :new.oldduedate;

    if :new.newduedate > :new.oldduedate then
        differencestatement := 'Project has been delayed';
    Else 
        differencestatement := 'Project has been scheduled to finish eariler';
    end if;

    :new.status := differencestatement;

end;

可能您应该给它一个更通用的名称。