3

i have 2 simple table

CREATE TABLE employee (
  emp_id INT PRIMARY KEY,
  first_name VARCHAR(40),
  last_name VARCHAR(40),
  birth_day DATE,
  sex VARCHAR(1),
  salary INT,
  super_id INT,
  branch_id INT
);

CREATE TABLE biodata (
  emp_id INT PRIMARY KEY,
  first_name VARCHAR(40),
  last_name VARCHAR(40),
  birth_day DATE,
  sex VARCHAR(1)
);

i want to merge it

merge into biodata c
using employee e
on (c.emp_id = e.emp_id)
when matched then
  update set
  c.emp_id=e.emp_id,
  c.first_name=e.first_name,
  c.last_name=e.last_name,
  c.birth_day=e.birth_day,
  c.sex=e.sex
when not matched then
  insert VALUES(e.emp_id,e.first_name,e.last_name,e.birth_day,e.sex);

but Oracle says:

ORA-38104: Columns referenced in the ON Clause cannot be updated: c.emp_id

Barbaros Özhan
  • 59,113
  • 10
  • 31
  • 55

1 Answers1

6

Just remove the c.emp_id=e.emp_id from UPDATE clause as it is irrelevant

(UPDATE will be done on the record of table biodata for which condition c.emp_id = e.emp_id is satisfied. so the record which is going to be updated already have the same emp_id as e.emp_id).

merge into biodata c
using employee e
on (c.emp_id = e.emp_id)
when matched then
  update set
  --c.emp_id=e.emp_id,
  c.first_name=e.first_name,
  c.last_name=e.last_name,
  c.birth_day=e.birth_day,
  c.sex=e.sex
when not matched then
  insert VALUES(e.emp_id,e.first_name,e.last_name,e.birth_day,e.sex);

Cheers!!

Popeye
  • 35,427
  • 4
  • 10
  • 31