在9i中使用MERGE

时间:2011-09-01 06:47:47

标签: oracle merge oracle9i

我想在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'
                                       END;

请帮忙! Oracle 9i不允许我这样做,因为它不匹配时没有。如何在9i中实现相同的结果?

2 个答案:

答案 0 :(得分:0)

使用rowid方法:

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;

答案 1 :(得分:0)

Oracle 9i 确实具有WHEN NOT MATCHED子句。但是你无法使用更新。

在您的情况下,您不需要合并,UPDATE应该按照您的要求执行:

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')