变异 table - 触发错误
Mutating table - trigger error
我必须实现以下触发器:
The total number of votes, per election year, of the elections after 1960, does not exceed 538
但是,我得到了变异 table 错误。我明白为什么会出现错误,但我看不到其他解决方案(带触发器)。我可以创建一个临时的 table 但我只想拥有触发器。
这是代码:
CREATE OR REPLACE TRIGGER restrict_election_votes
after INSERT OR UPDATE ON election
for each row
declare
v_nbofvotes number;
v_eleyr election.election_year%type :=:NEW.election_year;
v_votes election.votes%type :=:NEW.VOTES;
begin
select sum(votes)
into v_nbofvotes
from election
where election_year=v_eleyr;
if(v_votes+v_nbofvotes >538)
THEN
RAISE_APPLICATION_ERROR(-20500, 'Too many votes');
END IF;
END;
update election
set votes=175
where candidate='MCCAIN J'
and election_year=2008;
你确定那里需要触发器吗?你可以用check constraint解决这个问题:
alter table election add consntraint too_many_votes check (votes < 538 or year < 1960);
假设问题是您需要查询选举table,因为总票数是根据多行确定的,那么如果您删除"for each row"并将其设为语句级触发器(将不得不更改查询以检查自 1960 年以来所有选举的 sum(votes) 规则,因为您不知道哪一行 inserted/updated)然后它将起作用。
create table mb_elct (year varchar2(4), cand varchar2(30), vt number)
create or replace trigger mb_elct_trg
after insert or update on mb_elct
declare
v_nbofvotes number;
begin
select count(*)
into v_nbofvotes
from (
select year, sum(vt)
from mb_elct
where year > '1960'
group by year
having sum(vt) >538
);
if(nvl(v_nbofvotes,0) != 0 )
THEN
RAISE_APPLICATION_ERROR(-20500, 'Too many votes');
END IF;
END;
/
insert into mb_elct values ('2008', 'McCain', 500);
1 row inserted
update mb_elct set vt = vt + 200 where year = '2008' and cand = 'McCain';
ORA-20500: Too many votes
ORA-06512: at "EDR_ADMIN.MB_ELCT_TRG", line 16
ORA-04088: error during execution of trigger 'EDR_ADMIN.MB_ELCT_TRG'
我必须实现以下触发器:
The total number of votes, per election year, of the elections after 1960, does not exceed 538
但是,我得到了变异 table 错误。我明白为什么会出现错误,但我看不到其他解决方案(带触发器)。我可以创建一个临时的 table 但我只想拥有触发器。 这是代码:
CREATE OR REPLACE TRIGGER restrict_election_votes
after INSERT OR UPDATE ON election
for each row
declare
v_nbofvotes number;
v_eleyr election.election_year%type :=:NEW.election_year;
v_votes election.votes%type :=:NEW.VOTES;
begin
select sum(votes)
into v_nbofvotes
from election
where election_year=v_eleyr;
if(v_votes+v_nbofvotes >538)
THEN
RAISE_APPLICATION_ERROR(-20500, 'Too many votes');
END IF;
END;
update election
set votes=175
where candidate='MCCAIN J'
and election_year=2008;
你确定那里需要触发器吗?你可以用check constraint解决这个问题:
alter table election add consntraint too_many_votes check (votes < 538 or year < 1960);
假设问题是您需要查询选举table,因为总票数是根据多行确定的,那么如果您删除"for each row"并将其设为语句级触发器(将不得不更改查询以检查自 1960 年以来所有选举的 sum(votes) 规则,因为您不知道哪一行 inserted/updated)然后它将起作用。
create table mb_elct (year varchar2(4), cand varchar2(30), vt number)
create or replace trigger mb_elct_trg
after insert or update on mb_elct
declare
v_nbofvotes number;
begin
select count(*)
into v_nbofvotes
from (
select year, sum(vt)
from mb_elct
where year > '1960'
group by year
having sum(vt) >538
);
if(nvl(v_nbofvotes,0) != 0 )
THEN
RAISE_APPLICATION_ERROR(-20500, 'Too many votes');
END IF;
END;
/
insert into mb_elct values ('2008', 'McCain', 500);
1 row inserted
update mb_elct set vt = vt + 200 where year = '2008' and cand = 'McCain';
ORA-20500: Too many votes
ORA-06512: at "EDR_ADMIN.MB_ELCT_TRG", line 16
ORA-04088: error during execution of trigger 'EDR_ADMIN.MB_ELCT_TRG'