【问题标题】:Call Procedure Unexpected error调用过程意外错误
【发布时间】:2017-02-06 23:14:20
【问题描述】:

我正在编写一个 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;

甲骨文错误:

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.

【问题讨论】:

    标签: oracle stored-procedures plsql package


    【解决方案1】:

    employees 表 (in the default HR schema) 中的commission_pct 列定义为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) &lt;=5 检查失败,引发的异常并不能说明这一点——它仅指可能实际有效的员工 ID。无论如何,长度检查并没有真正意义。

    【讨论】:

    • 我明白了。我检查了commission_pct 列,我认为99.99 是该列的最大值。谢谢先生的帮助。我投票给你的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-20
    • 1970-01-01
    • 2015-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多