Oracle MERGE不会INSERT

时间:2010-02-25 17:54:56

标签: sql oracle merge replace upsert

我有这个简单的例子,我似乎无法工作:

MERGE INTO mytable  mt
USING dual
ON (mt.id = 'AAA' )  
WHEN MATCHED THEN 
    UPDATE SET mt.name = 'updated'
WHEN NOT MATCHED THEN 
    INSERT (mt.id , mt.name )
    VALUES ('AAA', 'Gooood' );

如果表中存在“AAA”记录,则会成功更新。

但如果不存在,则不插入:

Affected rows: 0
Time: 0.003ms

关于我做错了什么的任何线索?

1 个答案:

答案 0 :(得分:8)

适合我:

SQL> create table mytable (id varchar(3), name varchar(30));

Table created.

SQL> MERGE INTO mytable  mt
  2  USING dual
  3  ON (mt.id = 'AAA' )  
  4  WHEN MATCHED THEN 
  5      UPDATE SET mt.name = 'updated'
  6  WHEN NOT MATCHED THEN 
  7      INSERT (mt.id , mt.name )
  8      VALUES ('AAA', 'Gooood' );

1 row merged.

SQL> select * from mytable;

ID  NAME
--- ------------------------------
AAA Gooood