oracle如果行不存在则插入

问题描述:

insert ignore into table1 
select 'value1',value2 
from table2 
where table2.type = 'ok'

运行此命令时,出现错误缺少INTO关键字".

When I run this I get the error "missing INTO keyword" .

有什么想法吗?

运行此命令时,出现错误缺少INTO关键字".

When I run this I get the error "missing INTO keyword" .

因为IGNORE不是Oracle中的关键字.那就是MySQL语法.

Because IGNORE is not a keyword in Oracle. That is MySQL syntax.

您可以使用MERGE.

What you can do is use MERGE.

merge into table1 t1
    using (select 'value1' as value1 ,value2 
           from table2 
           where table2.type = 'ok' ) t2
    on ( t1.value1 = t2.value1)
when not matched then
   insert values (t2.value1, t2.value2)
/

在Oracle 10g中,我们可以使用合并而不处理两个分支.在9i中,我们必须使用虚拟" MATCHED分支.

From Oracle 10g we can use merge without handling both branches. In 9i we had to use a "dummy" MATCHED branch.

在更古老的版本中,唯一的选择是:

In more ancient versions the only options were either :

  1. 在发出INSERT(或在子查询中)之前测试行的存在;
  2. 使用PL/SQL执行INSERT并处理所有由此产生的DUP_VAL_ON_INDEX错误.