2016-06-12 54 views
1

錯誤:在Oracle 11g中得到錯誤

ORA-00932: inconsistent datatypes: expected NUMBER got CHAR

SQL:

SELECT CASE WHEN 
(
SELECT COUNT(COMP_CODE) FROM COMPANY_MASTER 
)=0 THEN 1 
ELSE 
(
SELECT LPAD((SELECT MAX(COMP_CODE)+1 FROM COMPANY_MASTER),3, 0) NOS FROM DUAL 
) 
END AS DOCNO FROM DUAL; 

回答

3

中的if-else的兩個分支應返還相同種類。如果else分支返回一個字符串(這是返回值lpad),那麼if分支也應該如此。例如:

SELECT CASE WHEN 
(
SELECT COUNT(COMP_CODE) FROM COMPANY_MASTER 
)=0 THEN '1' -- '1' is a string literal 
ELSE 
(
SELECT LPAD((SELECT MAX(COMP_CODE)+1 FROM COMPANY_MASTER),3, 0) NOS FROM DUAL 
) 
END AS DOCNO FROM DUAL;