Oracle身份专栏

时间:2019-02-14 04:23:40

标签: database oracle

具有标识列的oracle表是否只能一一插入记录? 如何为带有标识列的表插入多行?

我的oracle版本是:

Oracle Database 12c标准版12.1.0.2.0版-64位生产 PL / SQL版本12.1.0.2.0-生产 “ CORE 12.1.0.2.0生产” 适用于64位Windows的TNS:版本12.1.0.2.0-生产 NLSRTL版本12.1.0.2.0-生产

这是我的脚本来重现此问题

    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.

但是如果我使用union all插入相同的2行,则会发生错误:

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.

该错误让我感到困惑,不是ID列是自动生成的吗?

问了这个问题之后,我尝试了一下,它就可以了,所以它一定是一个oracle错误:

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.

1 个答案:

答案 0 :(得分:1)

这可能是Oracle 12.1中的错误。当我在Oracle 12.2中进行尝试时,收到列定义不明确的错误

如果我对第一个选择查询中的所有列使用适当的别名,则可以正常工作

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
;