插入或更新(如果已存在)

时间:2017-06-28 11:57:09

标签: oracle sql-merge

merge into bonuses 
using( select * from bonuses)s ON s.employee_id = '111'   
WHEN MATCHED THEN update set bonus='555' 
WHEN NOT MATCHED THEN insert insert into BONUSES (employee_id) values(115)

表的插入查询是

insert into BONUSES (employee_id) values(111)
insert into BONUSES (employee_id) values(112)
insert into BONUSES (employee_id) values(113)
insert into BONUSES (employee_id) values(114)
insert into BONUSES (employee_id) values(115)

如果employee_id = 111已经存在,则应该更新else 它应该插入。 如果有人知道,请帮助

3 个答案:

答案 0 :(得分:1)

类似的东西:

MERGE INTO bonuses dst
USING ( SELECT '111' AS employee_id, '555' AS bonus FROM DUAL ) src
ON ( dst.employee_id = src.employee_id )
WHEN MATCHED THEN
  UPDATE SET bonus = src.bonus
WHEN NOT MATCHED THEN
  INSERT ( employee_id, bonus )
  VALUES ( src.employee_id, src.bonus );

答案 1 :(得分:1)

您的语句有两个语法错误。

  • 您重复了insert个关键字。
  • 您错过了on子句条件的括号。这些是必需的,与普通from子句中的连接条件不同。

所以你的代码应该是这样的:

merge into bonuses b
using( select * from bonuses) s 
ON (s.employee_id = 115)  
WHEN MATCHED THEN update set bonus='555' 
WHEN NOT MATCHED THEN insert(employee_id) values(115)
/

但是,在using子句中包含目标表是没有意义的。它不会产生您认为会产生的结果......

SQL> select * from bonuses;

EMPLOYEE_ID      BONUS
----------- ----------
        111
        112
        113
        114
        115

5 rows selected.

SQL> merge into bonuses b
  2  using( select * from bonuses) s 
  3  ON (s.employee_id = 115)
  4  WHEN MATCHED THEN update set bonus='555' 
  5  WHEN NOT MATCHED THEN insert (employee_id) values(115)
  6  /  

9 rows merged.

SQL> select * from bonuses;

EMPLOYEE_ID      BONUS
----------- ----------
        111        555
        112        555
        113        555
        114        555
        115        555
        115
        115
        115
        115

9 rows selected.

SQL> 

也许这样的事适合你?

merge into bonuses b
using( select * from employees) e 
    ON ( b.employee_id = e.employee_id ) 
WHEN MATCHED THEN 
    update set bonus= 555 
WHEN NOT MATCHED THEN 
    insert (employee_id) values (e.id)

如果您没有与BONUSES表不同的员工ID来源,您可以使用DUAL表来伪造它:

SQL> merge into bonuses b
  2  using( select 115 as employee_id, 555 as bonus from dual union all
  3         select 116 as employee_id, 555 as bonus from dual) e 
  4      ON ( b.employee_id = e.employee_id ) 
  5  WHEN MATCHED THEN 
  6      update set bonus= e.bonus 
  7  WHEN NOT MATCHED THEN 
  8      insert (employee_id) values (e.employee_id) 
  9  /

2 rows merged.

SQL> select * from bonuses;

EMPLOYEE_ID      BONUS
----------- ----------
        111
        112
        113
        114
        115        555
        116

6 rows selected.

SQL> 

答案 2 :(得分:0)

我认为你所追求的是:

merge into bonuses tgt
using (select '111' employee_id, '555' bonus from dual) src
  on (tgt.employee_id = src.employee_id)
WHEN MATCHED THEN
  update set tgt.bonus = src.bonus 
WHEN NOT MATCHED THEN
  insert (tgt.employee_id, tgt.bonus)
  values (src.employee_id, src.bonus);

顺便说一下,为什么要将字符串插入通常具有某种形式NUMBER的数据类型的列中?这些列是否真的具有字符串数据类型(例如VARCHAR2,CHAR等)?