繁体   English   中英

Oracle-合并中的子查询减少

[英]Oracle - Subqueries reduction in Merge

我正在合并带有要获取和ID的subqueryMERGE ,我想知道subquery in the NOT MATCHED statementsubquery in the NOT MATCHED statement是否始终执行。

        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

无需子查询,您只需在insert部分中引用pro.id ,如下所示:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM