我希望在某些条件为真时插入触发器插入表之前禁用
create or replace trigger CHECK_FOR_MAX_ENTRANTS
before insert on application
declare
entrants_count number;
max_entrants number;
begin
select count(*) into entrants_count from application
where id_speciality = :new.id_speciality;
select max_students_number into max_entrants from speciality s
where s.id_speciality = :new.id_speciality;
IF entrants_count >= max_entrants THEN
**disable this insert**
end;
我该怎么做?
答案 0 :(得分:3)
假设您正在谈论Oracle,那么,您可以代替禁用此插入:
IF entrants_count >= max_entrants THEN
raise_application_error(-21010, 'Max number of Entrants Reached');
END IF;
请参阅:http://download.oracle.com/docs/cd/B19306_01/appdev.102/b14261/errors.htm#sthref2006
编辑:让插入无声地失败(你要求的)通常是一个坏主意。如果您尝试在后插入触发器中删除记录,也可能会遇到变异表错误。
相反,只是不要插入记录开头。 您可能实现此目的的一种方法是在insert语句的末尾添加类似的内容:
WHERE EXISTS SELECT null FROM
(SELECT COUNT(*) entrants_count FROM application
WHERE id_speciality = :new.id_speciality) a,
(SELECT max_students_number max_entrants
FROM speciality WHERE id_speciality = :new.id_speciality) s
WHERE a.entrants_count < s.max_entrants
这应该只在entrants_count&lt;时执行insert语句。 max_entrants(你想要的)。
答案 1 :(得分:2)
尝试引发错误:
IF entrants_count >= max_entrants THEN
raise_application_error(-20001, 'Cannot perform this insert!');
END IF;