2013-04-10 51 views
0

爲什麼我會收到錯誤消息?爲什麼我收到錯誤ORA-01427:單行子查詢返回多行

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') 
+5

因爲你的子查詢返回多個行。 – swasheck 2013-04-10 15:55:51

+0

如果你從'SET'運行'SELECT',你會返回多少行? – Taryn 2013-04-10 15:56:48

+0

@swasheck你能爲此展示一些證據嗎? – Kermit 2013-04-10 15:58:02

回答

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。

2

我懷疑你真的想要聲明的SET部分中的子選擇。嘗試這樣的:

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') 
相關問題