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