Неверный триггер, не могу найти решение - PullRequest
0 голосов
/ 06 сентября 2018

Я хотел бы сделать триггер, который не позволяет человеку получать комиссию, превышающую 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.

1 Ответ

0 голосов
/ 06 сентября 2018

Хотя ваш комментарий говорит, что работает как шарм , рассмотрите возможность его упрощения - не нужно использовать локальную переменную или отличать импорт от обновления.

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>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...