使用Oracle在where where condition中使用'select query'更新表中的值

时间:2017-01-06 04:40:26

标签: oracle

我有这样的查询

    SELECT DISTINCT  A.BRANCH_ID,
               A.BRANCH_FINANCE_ID,
               A.PRODUCT_ID,
               A.INSENTIF_AMOUNT,
               MAX (A.TGL_START) TGL_START,
               B.BRANCH_NAME
        FROM      MST_INSENTIF_OWNER A
               LEFT JOIN
                  MST_BRANCH_FINANCE_COMPANY B
               ON A.BRANCH_FINANCE_ID = B.BRANCH_FINANCE_ID
       WHERE       A.BRANCH_ID = 002
               AND B.BRANCH_NAME LIKE '%ADIRA%'
               AND A.BRANCH_FINANCE_ID = 'FIN.13.00000054'
    GROUP BY   A.BRANCH_ID,
               A.BRANCH_FINANCE_ID,
               A.PRODUCT_ID,
               A.INSENTIF_AMOUNT,
               B.BRANCH_NAME

它显示总共174行,然后我想像这样从该表进行更新

     /* Formatted on 1/6/2017 11:33:51 AM (QP5 v5.115.810.9015) */
UPDATE   MST_INSENTIF_OWNER S
   SET   INSENTIF_AMOUNT = 1
 WHERE   EXISTS
            (  SELECT   A.BRANCH_ID,
                        A.BRANCH_FINANCE_ID,
                        A.PRODUCT_ID,
                        A.INSENTIF_AMOUNT,
                        MAX (A.TGL_START) TGL_START,
                        B.BRANCH_NAME
                 FROM      MST_INSENTIF_OWNER A
                        LEFT JOIN
                           MST_BRANCH_FINANCE_COMPANY B
                        ON A.BRANCH_FINANCE_ID = B.BRANCH_FINANCE_ID
                WHERE       A.BRANCH_ID = 002
                        AND B.BRANCH_NAME LIKE '%ADIRA%'
                        AND A.BRANCH_FINANCE_ID = 'FIN.13.00000054'
             GROUP BY   A.BRANCH_ID,
                        A.BRANCH_FINANCE_ID,
                        A.PRODUCT_ID,
                        A.INSENTIF_AMOUNT,
                        B.BRANCH_NAME)

但它也会使MST_INSENTIF_OWNER中的所有记录更新。这个查询我错了什么? :|

1 个答案:

答案 0 :(得分:0)

我不知道为什么Oracle无法使用join语句执行更新。然后,当我从论坛获得这个MERGE时,它节省了我的一天。

 /* Formatted on 1/6/2017 11:53:28 AM (QP5 v5.115.810.9015) */
    MERGE INTO   MST_INSENTIF_OWNER trg
         USING   (  SELECT   A.BRANCH_ID,
                             A.BRANCH_FINANCE_ID,
                             A.PRODUCT_ID,
                             A.INSENTIF_AMOUNT,
                             MAX (A.TGL_START) TGL_START,
                             B.BRANCH_NAME
                      FROM      MST_INSENTIF_OWNER A
                             LEFT JOIN
                                MST_BRANCH_FINANCE_COMPANY B
                             ON A.BRANCH_FINANCE_ID = B.BRANCH_FINANCE_ID
                     WHERE       A.BRANCH_ID = 002
                             AND B.BRANCH_NAME LIKE '%ADIRA%'
                             AND A.BRANCH_FINANCE_ID = 'FIN.13.00000054'
                  GROUP BY   A.BRANCH_ID,
                             A.BRANCH_FINANCE_ID,
                             A.PRODUCT_ID,
                             A.INSENTIF_AMOUNT,
                             B.BRANCH_NAME) src
            ON   (    trg.BRANCH_ID = src.BRANCH_ID
                  AND trg.BRANCH_FINANCE_ID = src.BRANCH_FINANCE_ID
                  AND trg.PRODUCT_ID = src.PRODUCT_ID
                  AND trg.TGL_START = src.TGL_START)
    WHEN MATCHED
    THEN
       UPDATE SET trg.INSENTIF_AMOUNT = 123;