触发器中ORA-06512中的未识别错​​误

时间:2018-06-26 01:46:20

标签: oracle triggers

怎么了?一切似乎都很好。但是...

CREATE OR REPLACE TRIGGER salary_change
BEFORE UPDATE
OF emp_salary
ON employee
FOR EACH ROW
WHEN (((NEW.emp_salary-OLD.emp_salary)/OLD.emp_salary)>0.2)
DECLARE
    limit NUMBER(7);
BEGIN
    limit:=:OLD.emp_salary*1.2;
    RAISE_APPLICATION_ERROR (-20000,'rule violated. cannot increase beyond : '|| limit);
END;

我有错误:

  

第3行出现错误:        ORA-20000:违反规则。不能超过:3360        ORA-06512:位于“ SYSTEM.SALARY_CHANGE”的第5行        ORA-04088:执行触发器“ SYSTEM.SALARY_CHANGE”时发生错误

2 个答案:

答案 0 :(得分:0)

ORA-06512只是告诉您代码中的哪一行导致了错误。在这种情况下,它是触发器中的第5行,显然与RAISE_APPLICATION_ERROR调用相对应。

因此,显然((NEW.emp_salary - OLD.emp_salary) / OLD.emp_salary大于0.2。假设消息中的限制值为3360,则这意味着OLD.EMP_SALARY为2800,而NEW.EMP_SALARY大于3360。

祝你好运

答案 1 :(得分:0)

USING RAISE_APPLICATION_ERROR ALSO GIVES ERROR ORA-06512 AND POSSIBLY ORA-04088

CREATE OR REPLACE TRIGGER hr.salary_change
    before UPDATE
    OF salary
    ON hr.employees
    FOR EACH ROW
    WHEN (((NEW.salary-OLD.salary)/OLD.salary)>0.2)
    DECLARE
        limit NUMBER(8,2);
        salary_high  exception;
        pragma exception_init(salary_high ,-20001);
BEGIN
        limit:=:OLD.salary*1.2;

        RAISE_APPLICATION_ERROR (-20001,' rule violated. cannot increase beyond :'||to_char(limit));

        exception
        when   salary_high  then
        :NEW.salary:=:OLD.salary;
        dbms_output.put_line(dbms_utility.format_error_stack);
END;

SQL> SET SERVEROUT ON
SQL> select salary  from hr.employees where employee_id=198;

    SALARY
----------
      3900

SQL> update  hr.employees set salary=5900 where employee_id=198;
ORA-20001:  rule violated. cannot increase beyond :4680
1 row updated.

SQL> select salary  from hr.employees where employee_id=198;

    SALARY
----------
      3900

SQL>