using MERGE in 9i
I want to do something like this in 9i
MERGE INTO SAT_DEMANDS D
USING (SELECT DISTINCT d1.PRODUCT_ID AS product_id, CA.CUST_ID as allocated_cust
FROM CDP_MPS_ALLOCATION CA, sat_demands d1
WHERE substr(d1.product_id,1,instr(d1.product_id,'_VIRTUAL',1)-1) = ca.prod_id(+)) MC
ON (D.PRODUCT_ID = MC.product_id AND D.CUSTOMER_ID = MC.allocated_cust)
WHEN matched then
Update set D.alloc_cust_id_s = CASE WHEN mc.allocated_cust IS NULL THEN 'FREE'
ELSE mc.allocated_cust||':'||'FREE'
开发者_如何学Go END;
Please help! Oracle 9i does not allow me to do this since it does not have when not matched. how do I achieve the same result in 9i?
Using a rowid approach:
FOR r in (SELECT D.rowid, MC.allocated_cust FROM
(SELECT DISTINCT d1.PRODUCT_ID AS product_id, CA.CUST_ID as allocated_cust
FROM CDP_MPS_ALLOCATION CA, sat_demands d1
WHERE substr(d1.product_id,1,instr(d1.product_id,'_VIRTUAL',1)-1) = ca.prod_id(+)
) MC
join SAT_DEMANDS D
ON (D.PRODUCT_ID = MC.product_id AND D.CUSTOMER_ID = MC.allocated_cust)
)
LOOP
Update SAT_DEMANDS D
set D.alloc_cust_id_s = CASE WHEN r.allocated_cust IS NULL THEN 'FREE'
ELSE r.allocated_cust||':'||'FREE'
END
where rowid=r.rowid;
END LOOP;
Oracle 9i does have the WHEN NOT MATCHED clause. You can't use update however with it.
In your case you don't need merge, UPDATE should do what you want:
UPDATE sat_demands d
SET alloc_cust_id_s = nvl((SELECT mc.allocated_cust || ':FREE'
FROM mc
WHERE d.product_id = mc.product_id
AND d.customer_id = mc.allocated_cust),
'FREE')
精彩评论