如何使用oracle在单个查询中插入两个表?

时间:2017-01-13 09:02:50

标签: sql oracle

这里我想在两个表中插入数据,

1。 COMPONENT_MASTER

2。 COMPONENT_TO_ACTION

为此我写了两个插入的查询。但是有两个查询有问题。

查询是:

INSERT INTO COMPONENT_MASTER(COMPONENT_ID,ROW_ID,COMPONENT_IDENTIFICATION,COMPONENT_NAME,COMPONENT_TYPE,COMPONENT_STATE,FECHA_DE_CREACION,REVISION)
SELECT MAX(COMPONENT_ID) + 1,?,?,?,?,?,CURRENT_TIMESTAMP,? FROM COMPONENT_MASTER

第二

insert into COMPONENT_TO_ACTION (ORDER_NUMBER,ACTION_ID,COMPONENT_ID,FECHA_DE_CREACION,GRABADO_POR,STATUS,REVISION) 
select max(ORDER_NUMBER) +1,?,?,current_timestamp,?,?,? from COMPONENT_TO_ACTION.

两个查询都需要一次插入执行。所以两者都需要插入相同的component_id。

我可以编写一个查询来插入两个表。

1 个答案:

答案 0 :(得分:1)

关于您的问题多表插入语法如下:

INSERT ALL
into COMPONENT_MASTER(COMPONENT_ID,ROW_ID,COMPONENT_IDENTIFICATION,COMPONENT_NAME,COMPONENT_TYPE,COMPONENT_STATE,FECHA_DE_CREACION,REVISION)
              values (COMPONENT_ID,ROW_ID,COMPONENT_IDENTIFICATION,COMPONENT_NAME,COMPONENT_TYPE,COMPONENT_STATE,FECHA_DE_CREACION,REVISION)
into COMPONENT_TO_ACTION (ORDER_NUMBER,ACTION_ID,COMPONENT_ID,FECHA_DE_CREACION,GRABADO_POR,STATUS,REVISION) 
                  values (ORDER_NUMBER,ACTION_ID,COMPONENT_ID,FECHA_DE_CREACION,GRABADO_POR,STATUS,REVISION)
SELECT (select MAX(COMPONENT_ID) + 1 from COMPONENT_MASTER) as COMPONENT_ID,
       (select max(ORDER_NUMBER) + 1 from COMPONENT_TO_ACTION) as ORDER_NUMBER,
       ? as ACTION_ID,
       ? as GRABADO_POR,
       ? as STATUS
       ? as ROW_ID,
       ? as COMPONENT_IDENTIFICATION,
       ? as COMPONENT_NAME,
       ? as COMPONENT_TYPE,
       ? as COMPONENT_STATE,
       CURRENT_TIMESTAMP as FECHA_DE_CREACION,
       ? as REVISION 
  FROM DUAL;

但是大家建议你不要使用select max(...) + 1。 Oracle有特殊的对象来创建唯一的后续标识符序列。并且您不能在子查询中使用带有多重插入的序列,但您可以将其添加到values(...)子句中并将其与nextval和curval一起使用。关于插入订单的Tom Kyte said that you may rely。请更改查询以使用它,结果你应该得到类似的东西:

-- for example I create two sequence 
CREATE SEQUENCE COMPONENT_MASTER_seq
START WITH     100000
INCREMENT BY   1
NOCACHE
NOCYCLE;

CREATE SEQUENCE COMPONENT_TO_ACTION_seq
START WITH     100000
INCREMENT BY   1
NOCACHE
NOCYCLE;


 INSERT ALL
into COMPONENT_MASTER(COMPONENT_ID,ROW_ID,COMPONENT_IDENTIFICATION,COMPONENT_NAME,COMPONENT_TYPE,COMPONENT_STATE,FECHA_DE_CREACION,REVISION)
              values (-- /*COMPONENT_ID*/
                       COMPONENT_MASTER_seq.nextval,ROW_ID,COMPONENT_IDENTIFICATION,COMPONENT_NAME,COMPONENT_TYPE,COMPONENT_STATE,FECHA_DE_CREACION,REVISION)
into COMPONENT_TO_ACTION (ORDER_NUMBER,ACTION_ID,COMPONENT_ID,FECHA_DE_CREACION,GRABADO_POR,STATUS,REVISION) 
                  values (--/*ORDER_NUMBER*/ 
                          COMPONENT_TO_ACTION_seq.nextval  ,ACTION_ID,
                          --/*COMPONENT_ID*/
                          COMPONENT_MASTER_seq.curval,FECHA_DE_CREACION,GRABADO_POR,STATUS,REVISION)
SELECT --COMPONENT_MASTER_seq.nextval as COMPONENT_ID, -- Oracle prohibit to use sequence with multi table insert
       --COMPONENT_TO_ACTION_seq.nextval as ORDER_NUMBER, -- Oracle prohibit to use sequence with multi table insert
       ? as ACTION_ID,
       ? as GRABADO_POR,
       ? as STATUS
       ? as ROW_ID,
       ? as COMPONENT_IDENTIFICATION,
       ? as COMPONENT_NAME,
       ? as COMPONENT_TYPE,
       ? as COMPONENT_STATE,
       CURRENT_TIMESTAMP as FECHA_DE_CREACION,
       ? as REVISION 
  FROM DUAL;