MERGE中的WHERE陈述

时间:2019-02-15 08:13:14

标签: sql oracle merge

如果在目标表和源表中有条件,有人可以在MERGE语句的末尾解释在哪里工作吗?例如:

merge into target_table t
using source source_table s
on s.flield = t.field
when matched then update (...)
when not matched then insert (...)
where t.field != <value> and s.field != <value>

由于源字符串与t.field子句中没有目标表的任何字符串匹配,我无法得到when not matched的解析方式。

我做了一些测试,所以在我看来行将永远不会插入。

我想知道:是否会插入任何行?

1 个答案:

答案 0 :(得分:2)

来自the official Oracle documentation for MERGE statement

  

仅当指定条件为true时,才希望数据库执行更新操作,请指定where_clause。该条件可以引用数据源或目标表。如果条件不成立,则在将行合并到表中时,数据库将跳过更新操作。

要明确回答您的问题(“我想知道:是否插入行?”),答案是:否。

为什么?因为语法错误。

您正在t.field内使用目标表列(merge_insert_clause),这是不允许的,它会引发ORA-38102: Invalid column in the INSERT WHERE Clause错误:see this fiddle example!

从文档中

  

merge_insert_clause指定在ON子句的条件为false时要插入到目标表的列中的值。

如果您要更精确地描述何时插入以及何时要更新,那么我还将编辑答案并提供进一步的说明。

希望我能帮上忙!