在oracle 11g中获取错误

时间:2016-06-12 16:30:03

标签: sql oracle select oracle11g

错误:

  

ORA-00932:不一致的数据类型:预期NUMBER得到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;

1 个答案:

答案 0 :(得分:3)

if-else的两个分支都应该返回相同的类型。如果else分支返回一个字符串(这是lpad的返回值),那么if分支也应如此。 E.g:

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;