尝试从多个表插入时插入语句 Returns ORA-01427 错误

Insert Statement Returns ORA-01427 Error While Trying To Insert From Multiple Tables

我有这个 table F_Flight,我想从 3 个不同的 table 中插入它。第一、四、五列来自同一个,第二、三列来自不同tables。当我执行代码时,出现 "single-row subquery returns more than one row" 错误。

insert when 1 = 1 then into F_Flight (planeid, groupid, dateid, flightduration, kmsflown) values 
(planeid, (select b.groupid from BridgeTable b where exists (select p.p1id from pilotkeylookup p where b.pilotid = p.p1id)), 
(select dd.id from D_Date dd where exists (select p.launchtime from PilotKeyLookup p where dd."Date" = p.launchtime)),
flightduration, kmsflown) select * from PilotKeyLookup p;

您的子查询返回多行,这就是错误消息所说的。您尝试插入一行的各种数据位和子查询之间没有关联。

这可以作为一个更简单的 insert...select 连接来完成,例如:

insert into f_flight (planeid, groupid, dateid, flightduration, kmsflown)
select pkl.planeid, bt.groupid, dd.id, pkl.flightduration, pkl.kmsflown
from pilotkeylookup pkl
join bridgetable bt on bt.pilotid = pkl.p1id
join d_date dd on dd."Date" = pkl.launchtime;

这将主要的 PilotKeyLookup table 连接到您在子查询中使用的键上的其他两个。

存储 ID 值而不是实际日期是不寻常的,如果 launchtime 有一个时间部分——从名称上看很可能——并且你的 d_date 条目只是日期(即所有时间在午夜)那么你将找不到匹配项;你可能需要做:

join d_date dd on dd."Date" = trunc(pkl.launchtime);

这似乎也可能是一个视图,因为您正在存储重复数据 - f_flight 中的所有内容显然都可以从其他 table 中找到。