查询返回:ORA-01427 单行子查询返回多于一行

Posted

技术标签:

【中文标题】查询返回:ORA-01427 单行子查询返回多于一行【英文标题】:Query returning : ORA-01427 Single-row subquery returning more than one row 【发布时间】:2020-09-07 10:41:18 【问题描述】:

我正在尝试将下面的 Sybase 查询转换为 Oracle 查询。

update Product set pd.age = (case when pd.exittime!= null then (sysdate - 
pd.exittime)  
else ( case when pd.queue = dp.queue 
then (select (sysdate - pd.entrytime) from department dp1 where pd.id = 
dp1.id ) else 2 END) END)
from Product pd,department dp
where pd.id > 1
AND pd.id = dp.id
AND pd.status in('1','7','2','5')
AND pd.currentstatus = dp.currentstatus 
AND pd.activity= dp.activity;

但我尝试在转换后执行以下 Oracle 查询,但出现以下错误。

update Product pd set pd.age = (select (case when pd.exittime!= null then 
(sysdate - pd.exittime)   
else ( case when pd.queue = dp.queue 
then (select (sysdate - pd.entrytime) from department dp1 where pd.id = dp1.id 
 ) else 2 END) END)
from department dp
where pd.id > 1
AND pd.id = dp.id
AND pd.status in('1','7','2','5')
AND pd.currentstatus = dp.currentstatus 
AND pd.activity= dp.activity) 
where exists 
(select 1 from department dp
where pd.id > 1
AND pd.id = dp.id
AND pd.status in('1','7','2','5')
AND pd.currentstatus = dp.currentstatus 
AND pd.activity= dp.activity);

【问题讨论】:

【参考方案1】:

你可以试试下面,

我使用了coalesce,以防queue 列不匹配,它会将其视为空值并取而代之的是2

update product pd
set    pd.age = case
                   when pd.exittime != null then
                    (sysdate - pd.exittime)
                   else
                    coalesce((select (sysdate - pd.entrytime)
                                from department dp
                                where pd.queue = dp.queue
                                  and pd.id = dp.id)
                            ,2)
                end
where  pd.id > 1
and    pd.status in ('1','7','2','5')
and    exists (select 1
                 from department dp
                where pd.id = dp.id
                  and pd.currentstatus = dp.currentstatus
                  and pd.activity = dp.activity);

【讨论】:

【参考方案2】:

如之前的回答中所建议的,请使用密钥保留视图将此类 Sybase 更新查询转换为 Oracle,因为 Oracle 不允许在更新查询中进行 JOIN。

UPDATE (SELECT pd.age as age,
              pd.id,
              dp.id,
              pd.currentstatus,
              dp.currentstatus,
              pd.activity,
              dp.activity,
              pd.status,
              pd.exittime,
              pd.queue,
              dp.queue
              FROM Product pd INNER JOIN department dp
        ON (pd.id = dp.id AND pd.currentstatus = dp.currentstatus AND pd.activity= dp.activity)
        WHERE pd.id > 1 AND pd.status in('1','7','2','5'))
SET pd.age = CASE WHEN pd.exittime!= null THEN 1 
ELSE ( CASE WHEN pd.queue = dp.queue 
            THEN sysdate - pd.exittime
            ELSE 2 END) 
    END;

【讨论】:

以上是关于查询返回:ORA-01427 单行子查询返回多于一行的主要内容,如果未能解决你的问题,请参考以下文章

ORA-01427 单行子查询返回多于一行

Oracle SQL:ORA-01427:单行子查询返回多于一行

ORA-01427: 单行子查询返回多于一行 ,,WHEN USING SELECT COUNT

我得到那个错误 ORA-01427: 单行子查询返回多于一行

ORA-01427单行子查询返回多于 1 行

Oracle APEX 交互式报告中的错误 - ORA-01427:单行子查询返回多于一行