2014-01-09 105 views
0

我有一個EMPLOYEE表列(主鍵)EMPLOYEE_ID NUMBER(6)爲什麼我得到ORA-01438錯誤

而下面的方法來檢查表中是否存在員工記錄

public boolean exists(int employeeId) { 

     Connection con = ConnectionManager.getConnection(); 
     boolean exists = false; 
     String stmt = "select EMPLOYEE_ID , FIRST_NAME , LAST_NAME , EMAIL , PHONE_NUMBER ," 
       + "HIRE_DATE , JOB_ID , SALARY , COMMISSION_PCT , MANAGER_ID , DEPARTMENT_ID " 
       + " FROM EMPLOYEES where cast (EMPLOYEE_ID as Number(2)) = ?"; 
     PreparedStatement pstmt = null; 
     try { 
      pstmt = con.prepareStatement(stmt); 
      pstmt.setInt(1, employeeId); 
      pstmt.execute(); //Returns true if the first object that the query returns is a ResultSet object 
      exists = pstmt.getResultSet().next(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } finally { 
      try { 
       pstmt.close(); 
       con.close(); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
     return exists; 
    } 

現在,在SQL語句時,我使用cast (EMPLOYEE_ID as Number(2)) = ?並調用exists方法與輸入2 employeeOperations.exists(2);我收到以下錯誤

ORA-01438: value larger than specified precision allowed for this column

即使列的大小足夠大,爲什麼會發生此錯誤?

它適用於輸入4位或更多(表中employee_id的最大長度爲4),即cast (EMPLOYEE_ID as Number(4)) = ?的作品。

+4

爲什麼你鑄造號碼(2)?如果employee_id最多可以達到6,但爲什麼要施展? – tbone

回答

2

當從鑄造到NUMBER(6)NUMBER(2),具有大於99 EMPLOYEE_ID記錄被導致錯誤(例如Oracle無法放100\d{2})。

速戰速決,用這個請求:

SELECT COUNT(1) FROM EMPLOYEES WHERE EMPLOYEE_ID = ? 
+0

+1一個簡單的問題,我們可以讓列的大小大於cast函數中定義的列大小嗎?考慮到上述示例是否合法編寫投票(EMPLOYEE_ID爲數字(10))? – Nishant