输入如下触发器,报错“表发生了变化, 触发器/函数不能读它”
create or replace TRIGGER OBJ5_1
BEFORE INSERT OR UPDATE of sal
ON emp1
for each row
declare
s emp1.sal%type;
BEGIN
CASE
WHEN UPDATING('sal') THEN
select sal into s from emp1 where empno=:old.mgr;
IF :new.sal > s THEN
RAISE_APPLICATION_ERROR(-20001,'员工工资不得大于经理');
END IF;
WHEN INSERTING THEN
select sal into s from emp1 where empno=:new.mgr;
IF :new.sal > s THEN
RAISE_APPLICATION_ERROR(-20001, '员工工资不得大于经理');
END IF;
END CASE;
END;
update emp1 set sal=41750 where empno=7499;
错误分析: 出现以上异常是因为trigger中SQL语句不能进行读或者修改触发语句的所在的表。
解决办法:
create or replace TRIGGER OBJ5_1
BEFORE INSERT OR UPDATE of sal
ON emp1
for each row
declare
s emp1.sal%type;
PRAGMA AUTONOMOUS_TRANSACTION;
BEGIN
CASE
WHEN UPDATING('sal') THEN
select sal into s from emp1 where empno=:old.mgr;
IF :new.sal > s THEN
RAISE_APPLICATION_ERROR(-20001,'员工工资不得大于经理');
END IF;
WHEN INSERTING THEN
select sal into s from emp1 where empno=:new.mgr;
IF :new.sal > s THEN
RAISE_APPLICATION_ERROR(-20001, '员工工资不得大于经理');
END IF;
END CASE;
COMMIT;
END;
多了PRAGMA AUTONOMOUS_TRANSACTION;COMMIT;两句