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

佐手、 提交于 2021-02-16 21:32:36

问题


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


回答1:


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!!



来源:https://stackoverflow.com/questions/59550274/ora-38104-columns-referenced-in-the-on-clause-cannot-be-updated-c-emp-id

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!