How to INSERT multiple values based on multiple subselects

问题描述 投票:0回答:2

我们已经经历了很多线程,但似乎无法理解在我们的案例中究竟如何实现这一点。

我们希望有多个 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)

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

sql oracle sql-insert bulkinsert
2个回答
1
投票

您会将第二列添加到第一个选择

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)

0
投票

有两种方法可以使用

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 到很多)

您在问题中提供的示例语句以某种方式将这两种方法合并为一个(无效的)插入语句。

由于您要插入多行,并且有一个生成这些行的选择语句,因此您想使用第二种方法(根据@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
表及其连接中插入 ID(顺便说一句,您不需要全部 - 您没有使用
atransportordercustomfieldv
表/视图中的任何东西)那不是' t 已经出现在 atransportordercustomfieldv 表/视图中,也许像下面这样的东西可以解决问题:

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);
© www.soinside.com 2019 - 2024. All rights reserved.