带有表连接的Oracle sql更新给出了错误

时间:2015-12-11 13:27:42

标签: sql oracle sql-update

我正在运行以下Oracle更新语句并收到错误:

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

UPDATE Maximo.laborcraftrate lcr
SET lcr.controlaccount = (SELECT c.mxrcontrolaccount
                     FROM maximo.contract c
                     WHERE lcr.contractnum = c.CONTRACTNUM)
WHERE EXISTS (SELECT controlaccount
              FROM maximo.contract c
              WHERE lcr.contractnum = c.contractnum);   

有谁知道如何限制子查询?

编辑:

这是一个显示表之间连接的select语句。我想要做的是在这些条件下用b.controlaccount更新a.controlaccount:

CRAFT   CONTRACTNUM CONTROLACCOUNT  MXRCONTROLACCOUNT
TR-CHNDI    TR-GENERAL      01-3010-72515
TR-CHNDP             TR-GENERAL     01-3010-72515
TR-CHNDI             TR-GENERAL     01-3010-72515
TR-CHNDI             TR-GENERAL     01-3010-72515
TR-CHNDP             TR-GENERAL     01-3010-72515
TR-FENI          TR-GENERAL     01-3010-72515
select a.craft,a.contractnum,a.controlaccount,b.mxrcontrolaccount
from maximo.laborcraftrate a
left join maximo.contract b on a.contractnum = b.contractnum
where a.contractnum is not null

1 个答案:

答案 0 :(得分:1)

您对错误的理解不清楚?子查询返回多行。

一个简单的解决方案是只添加and rownum = 1

UPDATE Maximo.laborcraftrate lcr
    SET lcr.controlaccount = (SELECT c.mxrcontrolaccount
                              FROM maximo.contract c
                              WHERE lcr.contractnum = c.CONTRACTNUM AND rownum = 1
                             )
    WHERE EXISTS (SELECT controlaccount
                  FROM maximo.contract c
                  WHERE lcr.contractnum = c.contractnum);  

或者使用聚合功能。这两个都保证最多一行。但是,这个问题不一定适用于简单的修复。通常存在逻辑问题。