为什么我收到错误ORA-01427:单行子查询返回多行

时间:2013-04-10 15:53:50

标签: sql oracle toad

为什么我收到错误消息?

  

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

UPDATE JOB_TASK JT1
   SET JT1.job_task_status_id =
          (SELECT JTS1.JOB_TASK_STATUS_ID
             FROM MAXIMO_WO MW1
                  INNER JOIN job_task_status jts1
                     ON MW1.STATUS = jts1.description
                     INNER JOIN JOB_TASK JT1
                     ON jt1.job_task_id = mw1.wonum)
WHERE   JT1.JOB_TASK_STATUS_ID IN
          (SELECT JTS.JOB_TASK_STATUS_ID
             FROM JOB_TASK_STATUS JTS,
                  JOB_TASK JT,
                  MAXIMO_WO MW,
                  WORK_ORDER_CONTROL WOC
            WHERE     JT.JOB_TASK_ID = MW.WONUM
                  AND JT.JOB_TASK_STATUS_ID = JTS.JOB_TASK_STATUS_ID
                  AND JT.WORK_ORDER_ID = WOC.WORK_ORDER_ID
                  and jt.job_task_id = mw.wonum
                  AND MW.STATUS IN
                         ('APPR',
                          'SCHED',
                          'INPRG',
                          'FNACCEPT',
                          'WMATL',
                          'WKCOMP',
                          'COMP')
                  AND WOC.COMPANY_ID = '21'
                  AND MW.SITEID <> 'NUCDEV'
                  AND DECODE (
                         INSTR (JTS.DESCRIPTION, '-', 1),
                         0, JTS.DESCRIPTION,
                         SUBSTR (JTS.DESCRIPTION,
                                 1,
                                 (INSTR (JTS.DESCRIPTION, '-', 1) - 1))) <>
                         MW.STATUS
                  AND JT.LEVEL_NUMBER = '3'
                  AND MW.STATUSDATE < SYSDATE - 1
                  AND MW.WONUM <> 'UNDEFINED')

2 个答案:

答案 0 :(得分:2)

我怀疑你真的想要声明的SET部分中的subselect。尝试这样的事情:

 UPDATE JT1
    SET JT1.job_task_status_id = JTS1.JOB_TASK_STATUS_ID
 FROM MAXIMO_WO MW1
      INNER JOIN job_task_status jts1
            ON MW1.STATUS = jts1.description
      INNER JOIN JOB_TASK JT1
            ON jt1.job_task_id = mw1.wonum
 WHERE   JT1.JOB_TASK_STATUS_ID IN
      (SELECT JTS.JOB_TASK_STATUS_ID
         FROM JOB_TASK_STATUS JTS,
              JOB_TASK JT,
              MAXIMO_WO MW,
              WORK_ORDER_CONTROL WOC
        WHERE     JT.JOB_TASK_ID = MW.WONUM
              AND JT.JOB_TASK_STATUS_ID = JTS.JOB_TASK_STATUS_ID
              AND JT.WORK_ORDER_ID = WOC.WORK_ORDER_ID
              and jt.job_task_id = mw.wonum
              AND MW.STATUS IN
                     ('APPR',
                      'SCHED',
                      'INPRG',
                      'FNACCEPT',
                      'WMATL',
                      'WKCOMP',
                      'COMP')
              AND WOC.COMPANY_ID = '21'
              AND MW.SITEID <> 'NUCDEV'
              AND DECODE (
                     INSTR (JTS.DESCRIPTION, '-', 1),
                     0, JTS.DESCRIPTION,
                     SUBSTR (JTS.DESCRIPTION,
                             1,
                             (INSTR (JTS.DESCRIPTION, '-', 1) - 1))) <>
                     MW.STATUS
              AND JT.LEVEL_NUMBER = '3'
              AND MW.STATUSDATE < SYSDATE - 1
              AND MW.WONUM <> 'UNDEFINED')

答案 1 :(得分:0)

正如错误所示,SET语句中的子查询返回多个值:

SELECT JTS1.JOB_TASK_STATUS_ID
             FROM MAXIMO_WO MW1
                  INNER JOIN job_task_status jts1
                     ON MW1.STATUS = jts1.description
                     INNER JOIN JOB_TASK JT1
                     ON jt1.job_task_id = mw1.wonum

因为此查询未以任何方式连接到您的外部UPDATE语句。我知道,我知道,你为JOB_TASK表使用了相同的别名!但就Oracle而言,这只是巧合,因为您将JOB_TASK表重新加入了查询。此查询基本上将返回每个 job_task,其工作订单数据及其任务状态。

您需要的只是引用您的外部别名:

UPDATE JOB_TASK JT1
   SET JT1.job_task_status_id =
          (SELECT JTS1.JOB_TASK_STATUS_ID
             FROM MAXIMO_WO MW1
                  INNER JOIN job_task_status jts1
                     ON MW1.STATUS = jts1.description
                     and MW1.MoNum = jt1.job_task_id )
... 

这将正确使用您的外部JOB_TASK的job_task_id来获取其task_status_id。