2016-10-25 183 views
1

我想在同一張表中使用另一條記錄更新表中的一對列。以下是我想出的SQL,但我想知道如何避免多個子查詢返回相同的記錄。我正在致力於Oracle 11gR2避免多個子查詢

UPDATE 
    IFTBL E 
SET 
    E.ATT_CREATED = (SELECT A.CREATED FROM ATT A WHERE A.ROW_ID = E.T_ACTIVITYA__RID), 
    E.ATT_CREATED_BY = (SELECT B.CREATED_BY FROM ATT B WHERE B.ROW_ID = E.T_ACTIVITYA__RID) 
WHERE 
    E.IF_ROW_BATCH_NUM = BATCH_NO_IN AND E.IF_ROW_STAT = 'EXPORTED' AND E.ATT_FILE_SRC_TYPE = 'FILE'; 

回答

1

您可以使用合併。

MERGE INTO IFTBL 
    USING 
    (
     SELECT CREATED,CREATED_BY,ROW_ID 
     FROM ATT 
    ) A ON (A.ROW_ID = IFTBL.T_ACTIVITYA__RID) 
    WHEN MATCHED THEN UPDATE 
     SET 
     IFTBL.ATT_CREATED = A.CREATED, 
     IFTBL.ATT_CREATED_BY = A.CREATED_BY 
    WHERE 
    IFTBL.IF_ROW_BATCH_NUM = BATCH_NO_IN 
    AND IFTBL.IF_ROW_STAT = 'EXPORTED' 
    AND IFTBL.ATT_FILE_SRC_TYPE = 'FILE' 
0

如果ROW_ID是ATT主鍵和T_ACTIVITYA__RID是IFTBL參照外鍵ROW_ID您可以寫信更新(選擇...)

update (select E.ATT_CREATED, E.ATT_CREATED_BY 
       A.CREATED , A.CREATED_BY 
      from IFTBL E, ATT A 
     where E.T_ACTIVITYA__RID = A.ROW_ID(+) 
      AND E.IF_ROW_BATCH_NUM = BATCH_NO_IN 
      AND E.IF_ROW_STAT = 'EXPORTED' 
      AND E.ATT_FILE_SRC_TYPE = 'FILE') 
SET ATT_CREATED = CREATED, 
    ATT_CREATED_BY = CREATED_BY ; 
0

除了其他的答案,你可以簡單地做一個多列更新:

​​