Skip to content
Advertisement

Merge statement giving error on oracle when trying to merge two tables

i have a student table as :

enter image description here

I have a stud_claim table as is :

enter image description here

student_id is the foreign key of student table.

At stud_claim table, I have paid_date column .paid_date has some data and some row is null.I need to update stu_claim table where paid_date is null and put the value of the service_date in that paid_date,if paid_date is null.To update the paid_date,the another condition for them is to match the id of the student and stud_claim of stud_claim table.

I tried using merge statement but its not working:

MERGE INTO offc.stud_claim A
USING offc.student B
ON (A.student_id=B.id AND A.PAID_DATE IS NULL)
WHEN MATCHED THEN
UPDATE SET A.PAID_DATE=B.SERVICE_DATE

Or simply using update statement also ,it is also possible or not?

I got error at:

ORA-38104: Columns referenced in the ON Clause cannot be updated: "A"."PAID_DATE"

I tried from sql also as:

update offc.stud_claim a set paid_date=(select id from offc.student b where b.id=a.student_id) where a.paid_id is NULL;

Advertisement

Answer

Simple update statement will be enough for it. There is issue in your update statement.(you are selecting id instead of service_date)

Update offc.stud_claim a 
set paid_date=
(select service_date
   from offc.student b 
  where b.id=a.student_id) 
where a.paid_date is NULL;

In merge statement, you can not update the column which are used in on clause so you can achieve it using WHERE clause as following:

MERGE INTO offc.stud_claim A
USING offc.student B
ON (A.student_id=B.id)
WHEN MATCHED THEN
UPDATE SET A.PAID_DATE=B.SERVICE_DATE
WHERE A.PAID_DATE IS NULL;

Cheers!!

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement