0
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_T 中插入新的主记录但在 CHILD_COND 中没有相应的子记录时,S_TRD 被设置为 null

4

2 回答 2

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
于 2013-07-19T15:00:53.463 回答
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
于 2013-07-19T16:10:47.750 回答