Your statement has two syntax errors.
- You have repeated the
insert
keyword.
- You have missed the brackets around the
on
clause conditions. These are mandatory, unlike the join conditions in a normal from
clause.
So your code should look like this:
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)
/
However, it doesn't make sense to have the target table in the using
clause. It doesn't produce the results you think it's going to...
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>
Maybe something like this would suit you?
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)
If you don't have a source of employee IDs distinct from the BONUSES table you can use the DUAL table to fake it:
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>