2017-02-06 18 views
0

我正在編寫一個DML包,我想在包內創建一個簡單的過程來更新commission_pct值。我在執行包體時沒有收到任何語法錯誤,但是當我調用該過程時,我收到了一個意外的錯誤。謝謝。在一個簡單的PL/SQL塊調用過程意外的錯誤

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; 

呼叫過程:

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

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作爲UPDATE語句的一部分劃分:

SET  commission_pct = p_new_comm/100 

您可能要驗證在比檢查其長度其他一些方式傳遞的值。目前,如果LENGTH(p_new_comm) <=5檢查失敗,引發的異常並沒有說清楚 - 它只是指實際上可能有效的員工ID。但是,長度檢查並不是真的感覺到。

+0

我明白。我檢查了commission_pct專欄,我認爲99.99是該專欄的最大值。謝謝您的幫助。我投你的答案。 – Rattlesnake