我正在尝试查找我的数据库中是否有不止一个带触发器的总裁,如果是,则引发错误,我使用hr、表employees,并且我必须使用job_id来查找它。以下是我的代码。谢谢
CREATE OR REPLACE TRIGGER check_pres
BEFORE INSERT OR DELETE OR UPDATE ON employees
FOR EACH ROW
BEGIN
IF ((employees.job_id = 'AD_PRES') > 1)
THEN RAISE_APPLICATION_ERROR(-12345, 'More than one President in database.');
END IF;
END;
您应该通过删除FOR EACH ROW
表达式来使用Statement Level Trigger
而不是Row Level Trigger
CREATE OR REPLACE TRIGGER check_pres
BEFORE INSERT OR DELETE OR UPDATE ON employees
DECLARE
v_cnt int;
BEGIN
SELECT COUNT(*) INTO v_cnt FROM employees WHERE job_id = 'AD_PRES';
IF ( v_cnt > 1 ) THEN
RAISE_APPLICATION_ERROR(-20345, 'More than one President in database.');
END IF;
END;
否则,在获取计数值时会出现突变错误。顺便说一句,RAISE_APPLICATION_ERROR
的第一个自变量值应该在-20999
和-20000
之间