我想触发一个不允许某人的佣金高于其薪金50%的触发器。
我使用表emp
,其中包含列empno
,ename
,sal
和comm
。
这是我的代码:
set define off;
create or replace trigger block_prov
before update or insert on emp
for each row
declare
v_sal number;
begin
if inserting then
select sal into v_sal from emp where empno = :new.empno;
if :new.comm > v_sal*0.5 then
raise_application_error(-20001, 'prov greater than 50% of sal');
end if;
end if;
if updating then
if :new.comm > :old.sal*0.5 then
raise_application_error(-20001, 'prov greater than 50% of sal');
end if;
end if;
end;
问题在于,每次我尝试使用该行测试代码
update emp set comm = 700
where empno = 7499;
(它应该触发异常)
编译器给我这个错误信息:
*04098. 00000 - "trigger '%s.%s' is invalid and failed re-validation"
*Cause: A trigger was attempted to be retrieved for execution and was
found to be invalid. This also means that compilation/authorization
failed for the trigger.
*Action: Options are to resolve the compilation/authorization errors,
disable the trigger, or drop the trigger.
答案 0 :(得分:1)
尽管您的评论说它的工作原理很像,但请考虑简化它-无需使用局部变量或区分导入和更新。
SQL> create or replace trigger trg_block_prov
2 before update or insert on emp
3 for each row
4 begin
5 if :new.comm > :new.sal * 0.5 then
6 raise_application_error(-20001, 'prov greater than 50% of sal');
7 end if;
8 end;
9 /
Trigger created.
更新示例:
SQL> select ename, sal, comm From emp where ename = 'SMITH';
ENAME SAL COMM
---------- ---------- ----------
SMITH 800
SQL> update emp set comm = 500 where ename = 'SMITH';
update emp set comm = 500 where ename = 'SMITH'
*
ERROR at line 1:
ORA-20001: prov greater than 50% of sal
ORA-06512: at "SCOTT.TRG_BLOCK_PROV", line 3
ORA-04088: error during execution of trigger 'SCOTT.TRG_BLOCK_PROV'
SQL> update emp set comm = 300 where ename = 'SMITH';
1 row updated.
SQL> select ename, sal, comm From emp where ename = 'SMITH';
ENAME SAL COMM
---------- ---------- ----------
SMITH 800 300
插入示例:
SQL> insert into emp (empno, ename, sal, comm) values (1, 'lf', 1000, null);
1 row created.
SQL> insert into emp (empno, ename, sal, comm) values (2, 'bf', 1000, 700);
insert into emp (empno, ename, sal, comm) values (2, 'bf', 1000, 700)
*
ERROR at line 1:
ORA-20001: prov greater than 50% of sal
ORA-06512: at "SCOTT.TRG_BLOCK_PROV", line 3
ORA-04088: error during execution of trigger 'SCOTT.TRG_BLOCK_PROV'
SQL> insert into emp (empno, ename, sal, comm) values (2, 'bf', 1000, 400);
1 row created.
SQL> select ename, sal, comm From emp where empno in (1, 2);
ENAME SAL COMM
---------- ---------- ----------
lf 1000
bf 1000 400
SQL>