Oracle中的复杂更新查询问题

时间:2013-07-19 14:54:38

标签: oracle

UPDATE CHILD_COND
    SET S_TRD=
      (SELECT S_TRD
      FROM
        (SELECT MAX(TV.S_TRD)            AS S_TRD,
        TV.TRADEID                        AS TRADEID,
        TV.TRD_VER             AS TRD_VER,
        TV.TIMEST                 AS TIMEST,
        TV.SOURCENAME                     AS SOURCENAME,
        STG_TRADE.SP_COND AS SP_COND
        FROM MASTER_T TV,
        T_STG_TRADE_VERSION STG_TRADE
        WHERE TV.SOURCENAME       = STG_TRADE.SOURCENAME
        AND TV.TRADEID            = STG_TRADE.TRADEID
        AND TV.TRD_VER = STG_TRADE.TRD_VER
        AND TV.TIMEST     = STG_TRADE.TIMEST
        AND TRADESETID            = '91004'
        GROUP BY TV.TRADEID,
        TV.TRD_VER,
        TV.TIMEST,
        TV.SOURCENAME,
         STG_TRADE.SP_COND
        )TRD WHERE CHILD_COND.SP_COND = TRD.SP_COND AND S_TRD IS NOT NULL
     ) 

我需要根据master表中的主键更新子表中的外键。 但是当子表中没有值时,所有子外键值都将更新为null。 当在MASTER_T中插入新的主记录但在CHILD_COND中没有相应的子记录时,S_TRD被设置为null

2 个答案:

答案 0 :(得分:0)

MERGE语句将满足此要求,只有匹配且非空的S_TRD才会更新为child_cond

MERGE INTO CHILD_COND cc
     USING (  SELECT MAX (TV.S_TRD) AS S_TRD,
                     TV.TRADEID AS TRADEID,
                     TV.TRD_VER AS TRD_VER,
                     TV.TIMEST AS TIMEST,
                     TV.SOURCENAME AS SOURCENAME,
                     STG_TRADE.SP_COND AS SP_COND
                FROM MASTER_T TV, T_STG_TRADE_VERSION STG_TRADE
               WHERE     TV.SOURCENAME = STG_TRADE.SOURCENAME
                     AND TV.TRADEID = STG_TRADE.TRADEID
                     AND TV.TRD_VER = STG_TRADE.TRD_VER
                     AND TV.TIMEST = STG_TRADE.TIMEST
                     AND TRADESETID = '91004'
            GROUP BY TV.TRADEID,
                     TV.TRD_VER,
                     TV.TIMEST,
                     TV.SOURCENAME,
                     STG_TRADE.SP_COND) TRD
        ON (cc.SP_COND = trd.S_TRD 
        AND trd.S_TRD IS NOT NULL ) --remove this filter ,incase you need to copy the null s_trd value to child_cond when matched
WHEN MATCHED
THEN
   UPDATE SET cc.S_TRD = trd.S_TRD

答案 1 :(得分:0)

如果您不希望在CHILD_COND.SP_COND没有值的情况下将s_trd更新为null,则可以将where条件添加到更新语句中:

...
WHERE SP_COND IS NOT NULL

    UPDATE CHILD_COND
        SET S_TRD=
          (SELECT S_TRD
          FROM
            (SELECT MAX(TV.S_TRD)            AS S_TRD,
            TV.TRADEID                        AS TRADEID,
            TV.TRD_VER             AS TRD_VER,
            TV.TIMEST                 AS TIMEST,
            TV.SOURCENAME                     AS SOURCENAME,
            STG_TRADE.SP_COND AS SP_COND
            FROM MASTER_T TV,
            T_STG_TRADE_VERSION STG_TRADE
            WHERE TV.SOURCENAME       = STG_TRADE.SOURCENAME
            AND TV.TRADEID            = STG_TRADE.TRADEID
            AND TV.TRD_VER = STG_TRADE.TRD_VER
            AND TV.TIMEST     = STG_TRADE.TIMEST
            AND TRADESETID            = '91004'
            GROUP BY TV.TRADEID,
            TV.TRD_VER,
            TV.TIMEST,
            TV.SOURCENAME,
             STG_TRADE.SP_COND
            )TRD WHERE CHILD_COND.SP_COND = TRD.SP_COND AND S_TRD IS NOT NULL
         )
WHERE SP_COND IS NOT NULL