如果子字段为NULL,则用父字段更新子字段

时间:2019-02-15 10:51:56

标签: firebird

我有两个表PURCHASE_ORDER_HDRPURCHASE_ORDER_DET。已与键POH_ID关联。

页眉和详细信息表均包含DELIVERYDATE,其中详细信息的“交付日期”可以为空。

我想用标题的交付日期更新PURCHASE_ORDER_DET,其中明细的交付日期为NULL

尝试了类似以下的内容,这在语法上是不正确的。

UPDATE PURCHASE_ORDER_DET pod
INNER JOIN
(
    SELECT poh.POH_ID as parentId,
           poh.DELIVERYDATE as dlvDate
    FROM PURCHASE_ORDER_HDR poh 
) parent on pod.POH_ID = parent.parentId
SET 
    pod.DELIVERYDATE = parent.dlvDate
WHERE 
    pod.DELIVERYDATE = null

编辑

根据马克here的建议,我尝试了以下等效的MERGE语句。

UPDATE pod
SET pod.DELIVERYDATE = poh.DELIVERYDATE
FROM PURCHASE_ORDER_DET pod JOIN PURCHASE_ORDER_HDR poh ON poh.POH_ID = pod.POH_ID
WHERE pod.DELIVERYDATE = null 

等效的MERGE语句是

MERGE INTO PURCHASE_ORDER_DET AS pod 
    USING PURCHASE_ORDER_HDR AS poh
    ON poh.POH_ID = pod.POH_ID AND pod.DELIVERYDATE = NULL 
    WHEN MATCHED THEN 
        UPDATE SET pod.DELIVERYDATE = poh.DELIVERYDATE 

它确实执行但没有行生效。

1 个答案:

答案 0 :(得分:1)

谢谢马克,这对我有用。

MERGE INTO PURCHASE_ORDER_DET AS pod 
    USING PURCHASE_ORDER_HDR AS poh
    ON poh.POH_ID = pod.POH_ID AND pod.DELIVERYDATE is NULL 
    WHEN MATCHED THEN 
        UPDATE SET pod.DELIVERYDATE = poh.DELIVERYDATE