【问题标题】:Oracle - Subqueries reduction in MergeOracle - 合并中的子查询减少
【发布时间】:2016-09-29 13:53:58
【问题描述】:

我有一个MERGE 和一个subquery 来获取和ID,我想知道subquery 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。

【问题讨论】:

  • 不匹配时会执行。这里有什么疑问。您明确拒绝了您提到的连接满足时需要完成的活动。

标签: sql oracle merge oracle11gr2


【解决方案1】:

不需要子查询,你可以简单地在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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-27
    • 1970-01-01
    • 2014-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-06
    • 1970-01-01
    相关资源
    最近更新 更多