I've two tables PURCHASE_ORDER_HDR
and PURCHASE_ORDER_DET
. Linked with key POH_ID
.
Both Header and Detail table contains DELIVERYDATE
where detail's Delivery Date can be null.
I want to update PURCHASE_ORDER_DET
with Header's Delivery Date where Detail's Delivery Date is NULL
Tried the something like following, which is not syntactically correct.
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
EDIT
As suggested by Mark here, I tried the following with it's equivalent MERGE
statement.
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
The equivalent MERGE
statement is
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
It does execute but no row get effected.