问题前提:在一次日常开发中,使用UNION,UNION ALL 关联表插入数据时,报如下错误。
org.springframework.jdbc.BadSqlGrammarException:
### Error updating database. Cause: java.sql.SQLSyntaxErrorException: ORA-01790: expression must have same datatype as corresponding expression
表结构
create table TEST
(
ID VARCHAR2(32) not null,
CUST_NO VARCHAR2(32),
AMT NUMBER(16, 2)
)
产生问题的sql
单独执行此sql,并不报错。但是由代码执行时,则报错
INSERT INTO TEST (ID, CUST_NO, AMT)
select ID, CUST_NO, AMT
from ((select dbms_random.string('A',20) as ID,
dbms_random.string('A',20) as CUST_NO,
null as AMT
from dual)
UNION
(select dbms_random.string('A',20) as ID,
dbms_random.string('A',20) as CUST_NO,
1500 as AMT
from dual)
)
;
问题定位:经过排查发现,当批量插入时,数值类型中(我们遇到的是AMT字段),一条数据为null,其他数据不为null时报错则报此错误。给默认值解决