2016-09-29 77 views
0

我有一個MERGEsubquery獲取和ID,我想知道subquery in the NOT MATCHED statement總是執行或不。Oracle - 合併子查詢減少

 MERGE INTO CAR_STOCK st 
     USING CAR_PRODUCTO pro 
     ON (pro.id = st.producto_id AND pro.ean = ?) 
     WHEN MATCHED THEN 
     UPDATE SET st.stockActual = ? 
     WHEN NOT MATCHED THEN 
     INSERT (stockActual, local_id, producto_id, activo) 
     VALUES (?, ?, (SELECT id FROM car_producto WHERE ean = ?), 'S'); 

謝謝!

編輯:?是因爲我在PreparedStatement使用JDBC

+0

不匹配時,將執行。這裏有什麼疑問。當你提到的加入滿足時,你明確地否定了需要完成的活動。 – XING

回答

1

無需子查詢,你可以簡單地指pro.idinsert一部分,就像這裏:

merge into t1 using t2 on (t1.a = t2.a) 
    when matched then update set t1.b = t2.b 
    when not matched then insert (a, b) 
    values (0, t2.b) 

測試數據和merge結果:

create table t1 as (select 1 a, 'a01' b from dual); 
create table t2 as (select 1 a, 'a05' b from dual union all select 2 a, 'bxx' b from dual); 

    A B 
------ --- 
    1 a05 
    0 bxx