调用过程意外错误

时间:2017-02-06 23:14:21

标签: oracle stored-procedures plsql package

我正在编写DML包,我希望在包中创建一个简单的过程来更新commission_pct值。执行包体时,我没有收到任何语法错误,但是当我调用该过程时,我收到意外错误。感谢。

PROCEDURE update_commission_pct(
    p_empid     employees.employee_id%TYPE,
    p_new_comm  employees.commission_pct%TYPE
    )
  IS
    rec_confirm   employees%ROWTYPE;
    v_valid_empid BOOLEAN;
  BEGIN
    -- Simple boolean function to check employee existence
    v_valid_empid := dml_employees_pkg.check_employee_id(p_empid);

     IF
      v_valid_empid = TRUE 
      AND LENGTH(p_new_comm) <=5 THEN
        UPDATE    employees
        SET       commission_pct = p_new_comm
        WHERE     employee_id = p_empid
        RETURNING employee_id, commission_pct
        INTO      rec_confirm.employee_id, rec_confirm.commission_pct;

        DBMS_OUTPUT.PUT_LINE('Comission updated successfully.');
        DBMS_OUTPUT.PUT_LINE('Employee ID number ' ||
                             rec_confirm.employee_id || ' new comm is' ||
                             rec_confirm.commission_pct);
    ELSE
        RAISE_APPLICATION_ERROR(-20042, 'Employee ID ' || 
                                p_empid || ' Employee doesn't exist.');
    END IF;
  END update_commission_pct;

在简单的PL / SQL块中调用过程:

SET SERVEROUTPUT ON
BEGIN
  dml_employees_pkg.update_commission_pct(550, 10);
END;

Oracle错误:

Informe de error -
ORA-01438: value larger than specified precision allowed for this column
ORA-06512: at "HR.DML_EMPLOYEES_PKG", line 118
ORA-06512: at line 2
01438. 00000 -  "value larger than specified precision allowed for this column"
*Cause:    When inserting or updating records, a numeric value was entered
           that exceeded the precision defined for the column.
*Action:   Enter a value that complies with the numeric column's precision,
           or use the MODIFY option with the ALTER TABLE command to expand
           the precision.

1 个答案:

答案 0 :(得分:1)

commission_pct表(in the default HR schema)中的employees列定义为number(2, 2)

精度和比例is explained inthe documentation的含义,但实际上这意味着该列只能接受0.00到0.99之间的值。你试图插入10,正如错误所说的那样 - 超过允许的精度。

如果你想存储10%,你可以在程序调用中传递0.1作为第二个参数;或坚持传递10,但然后除以100作为更新声明的一部分:

SET       commission_pct = p_new_comm/100

您可能希望以某种方式验证传递的值,而不是检查其长度。目前,如果LENGTH(p_new_comm) <=5检查失败,则引发的异常并未明确说明 - 它仅指可能实际有效的员工ID。尽管如此,长度检查并不是真正意义上的。