【发布时间】:2019-06-17 16:50:43
【问题描述】:
我有删除触发器 我想删除警察,但是当我删除他时,他是他们老板的所有其他警察将在他们的老板字段中为空 所以我使用了这段代码
create or replace trigger switch_boss
before delete
on policeman
for each row
declare
boss number;
begin
boss := :new.bossid;
if(:new.policemanid = :new.bossid)then
select policemanid into boss from
(select * from policeman
order by dbms_random.value)
where rownum =1;
end if;
update policeman
set bossid = boss
where bossid = :new.policemanid;
end switch_boss;
我有错误
ORA-04091: table SYSTEM.POLICEMAN is mutating, trigger/function may not see it
ORA-06512: at "SYSTEM.SWITCH_BOSS", line 13
ORA-04088: error during execution of trigger 'SYSTEM.SWITCH_BOSS'
有什么想法吗?
更新: 我使用复合触发它的工作,但不是我想要的。 我想将被删除的警察的老板设置为他的老板。 问题是删除时我不能现在哪个警察将已删除的警察作为老板。 我可以找到它们,因为它们在删除后的字段中为空,但它们可能属于其他已删除的警察。
这是我做的触发器:
create or replace trigger switch_boss
for delete
on policeman
compound trigger
after statement is
cursor c is select * from policeman where bossid is null for update;
boss number;
begin
for r in c loop
select policemanid into boss from
(select * from policeman order by dbms_random.value)
where rownum =1;
update policeman
set bossid = boss
where current of c;
end loop;
end after statement;
end switch_boss;
【问题讨论】:
-
不要在
system或sys架构中创建对象(例如表)。 -
对了,建议你看看code indentation。此外,PL/SQL 不使用括号来表示
if条件,因为它有then关键字。
标签: sql oracle plsql database-trigger