oracle 如何基于多个子选择插入多个值

syqv5f0l  于 2023-05-16  发布在  Oracle
关注(0)|答案(2)|浏览(157)

我们已经经历了很多线程,但似乎不能理解如何在我们的情况下实现这一点。
我们希望有多个INSERTed值与1脚本。这些值取决于其他表的多个条件。我们得到的最好的结果如下:

insert into
atransportordercustomfieldv (ID,CFBOOLEAN2)
SELECT ((select transord.nr from transord
left join consignment on consignment.ASENDUNGID = transord.sendungid
left join order on consignment.akopfnr = order.nr
left join atransportordercustomfieldv af on transord.nr = af.id
where order.frachtagnr = 2418850 and transord.nr not in (select id from atransportordercustomfieldv WHERE ID not in (select transord.nr from transord
left join consignment on transord.ASENDUNGID = consignment .sendungid
left join order on consignment.akopfnr = order.nr
left join atransportordercustomfieldv af on transord.nr = af.id
where order.frachtagnr = 2418850))),0)

这不起作用,但它可以提供有关我们试图过滤的信息,因为它有点复杂。

kh212irz

kh212irz1#

将第二列添加到第一个选择中

insert into
atransportordercustomfieldv (ID,CFBOOLEAN2)
select transord.nr,0 
from transord
left join consignment on consignment.ASENDUNGID = transord.sendungid
left join order on consignment.akopfnr = order.nr
left join atransportordercustomfieldv af on transord.nr = af.id
where order.frachtagnr = 2418850 and transord.nr not in (select id from atransportordercustomfieldv WHERE ID not in (select transord.nr from transord
left join consignment on transord.ASENDUNGID = consignment .sendungid
left join order on consignment.akopfnr = order.nr
left join atransportordercustomfieldv af on transord.nr = af.id
where order.frachtagnr = 2418850)
wsewodh2

wsewodh22#

有两种方法可以使用insert语句将行插入到表中:

  • insert into <table_name> (<column_list>) values (<corresponding list of values>);-插入单行
  • insert into <table_name> (<column_list>) select <corresponding list of columns from <table(s)/subqueries/etc>;-插入select语句返回的行数(0到多行)

您在问题中提供的示例语句以某种方式将这两个方法合并到一个(无效)insert语句中。
由于您有多行要插入,并且有一个select语句生成这些行,因此您希望使用第二种方法(根据@nbk的答案):

INSERT INTO atransportordercustomfieldv (id, cfboolean2)
SELECT transord.nr,
       0
FROM   transord
LEFT   JOIN consignment
ON     consignment.asendungid = transord.sendungid
LEFT   JOIN ORDER ON consignment.akopfnr = order.nr
LEFT   JOIN atransportordercustomfieldv af
ON     transord.nr = af.id
WHERE  order.frachtagnr = 2418850
AND    transord.nr NOT IN (SELECT id
                           FROM   atransportordercustomfieldv
                           WHERE  id NOT IN (SELECT transord.nr
                                             FROM   transord
                                             LEFT   JOIN consignment
                                             ON     transord.asendungid = consignment.sendungid
                                             LEFT   JOIN ORDER ON consignment.akopfnr = order.nr
                                             LEFT   JOIN atransportordercustomfieldv af
                                             ON     transord.nr = af.id
                                             WHERE  order.frachtagnr = 2418850));

根据子查询中的逻辑是否正确,您可能可以在仅插入的merge语句中执行相同的操作,但看看您的查询,似乎不太可能。
但是,如果您的需求是从transord表及其连接(顺便说一句,您不需要所有这些-您没有使用atransportordercustomfieldv表/视图中的任何内容)中插入尚未存在于atransportordercustomfieldv表/视图中的id,则可能会有以下内容:

MERGE INTO atransportordercustomfieldv tgt
USING (SELECT transord.nr
       FROM   transord
       LEFT   JOIN consignment
       ON     consignment.asendungid = transord.sendungid
       LEFT   JOIN ORDER ON consignment.akopfnr = order.nr
       WHERE  order.frachtagnr = 2418850) src
  ON (tgt.id = src.nr)
WHEN MATCHED THEN
  INSERT (tgt.id, tgt.cfboolean2)
  VALUES (src.nr, 0);

相关问题