Reputation: 95
Is that the oracle table with identity column can only insert record one by one? How can I insert multiple rows for the table with identity column?
My oracle version is:
Oracle Database 12c Standard Edition Release 12.1.0.2.0 - 64bit Production PL/SQL Release 12.1.0.2.0 - Production "CORE 12.1.0.2.0 Production" TNS for 64-bit Windows: Version 12.1.0.2.0 - Production NLSRTL Version 12.1.0.2.0 - Production
Here is my script to reproduce this problem
CREATE TABLE T_BATCH_STATUS
(
ID INTEGER GENERATED ALWAYS AS IDENTITY NOT NULL
, TARGET_DATE TIMESTAMP NOT NULL
, JAVA_CLASS VARCHAR2(100) NOT NULL
, STATUS VARCHAR2(35) NOT NULL
, MSG VARCHAR2(1000)
, FAIL_CNT NUMBER(3) default 0 NOT NULL
, CREATE_USER VARCHAR2(35) NOT NULL
, CREATE_DATE TIMESTAMP NOT NULL
, UPDATE_USER VARCHAR2(35)
, UPDATE_DATE TIMESTAMP
, VERSION number(10) NOT NULL
, CONSTRAINT PK_BATCH_STATUS PRIMARY KEY (ID)
, CONSTRAINT U_BATCH_STATUS UNIQUE (TARGET_DATE, JAVA_CLASS)
)
;
insert into t_batch_status
(
TARGET_DATE
, JAVA_CLASS
, STATUS
, FAIL_CNT
, CREATE_USER
, CREATE_DATE
, VERSION
)
select to_date('2019.01.01', 'yyyy.MM.dd'), 'DownloadAndUnzip', 'NEW', 0, 'SYSTEM', sysdate, 0 from dual
;
1 row inserted
insert into t_batch_status
(
TARGET_DATE
, JAVA_CLASS
, STATUS
, FAIL_CNT
, CREATE_USER
, CREATE_DATE
, VERSION
)
select to_date('2019.02.14', 'yyyy.MM.dd'), 'DownloadAndUnzip', 'PENDING', 0, 'SYSTEM', sysdate, 0 from dual
;
1 row inserted.
But if I use union all to insert same 2 rows, the error occur:
truncate table t_batch_status;
insert into t_batch_status
(
TARGET_DATE
, JAVA_CLASS
, STATUS
, FAIL_CNT
, CREATE_USER
, CREATE_DATE
, VERSION
)
select to_date('2019.01.01', 'yyyy.MM.dd'), 'DownloadAndUnzip', 'NEW', 0, 'SYSTEM', sysdate, 0 from dual
union all select to_date('2019.02.14', 'yyyy.MM.dd'), 'DownloadAndUnzip', 'PENDING', 0, 'SYSTEM', sysdate, 0 from dual
;
Error report -
SQL Error: ORA-01400: cannot insert NULL into ("CYBERDB_DEV"."T_BATCH_STATUS"."ID")
01400. 00000 - "cannot insert NULL into (%s)"
*Cause: An attempt was made to insert NULL into previously listed objects.
*Action: These objects cannot accept NULL values.
The error make me confuse, isn't that the ID column is auto generated?
After asked this question, I tried this and it works, so it must be an oracle bug:
insert into t_batch_status
(
TARGET_DATE
, JAVA_CLASS
, STATUS
, FAIL_CNT
, CREATE_USER
, CREATE_DATE
, VERSION
)
with tmp as (
select b.base + rownum target_date
from all_objects a
, (select to_date('2019.01.01', 'yyyy.MM.dd') base from dual) b
where rownum < 42
)
select target_date, 'DownloadAndUnzip' as JAVA_CLASS , 'NEW' as STATUS
, 0 as FAIL_CNT , 'SYSTEM' as CREATE_USER , sysdate as CREATE_DATE, 0 as VERSION
from tmp
;
41 rows inserted.
Upvotes: 0
Views: 1192
Reputation: 31676
It may be a bug in Oracle 12.1. When I tried it in Oracle 12.2, I receive column ambiguously defined error.
If I use proper aliases to all the columns in the first select query, it works fine
insert into t_batch_status
(
TARGET_DATE
, JAVA_CLASS
, STATUS
, FAIL_CNT
, CREATE_USER
, CREATE_DATE
, VERSION
)
select to_date('2019.01.01', 'yyyy.MM.dd') as TARGET_DATE ,
'DownloadAndUnzip' as JAVA_CLASS , 'NEW' as STATUS
, 0 as FAIL_CNT , 'SYSTEM' as CREATE_USER , sysdate as CREATE_DATE, 0 as VERSION
from dual
union all
select to_date('2019.02.14', 'yyyy.MM.dd'), 'DownloadAndUnzip', 'PENDING',
0, 'SYSTEM', sysdate, 0 from dual
;
Upvotes: 1