Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merge, when matched insert

I wish to perform constructive merge in Oracle i.e. if a matching record is found in the source table, a new record with current timestamp should be added to the destination table.

How do I do this using merge in Oracle? Below is my code which gives a "missing keyword" error.

merge into studLoad sl
using student s
on(s.studID=sl.studID)
when matched
then
insert(sl.studID,sl.studName)
values(s.studID||'abc',s.studName)
when not matched
then
insert(sl.studID,sl.studName)
values(s.studID,s.studName);

Also, I dont know how to add current timestamp along with the studName. Any suggestions for the same would be welcome.

like image 969
z22 Avatar asked Feb 24 '26 22:02

z22


1 Answers

This shouldn't be done with a merge (as Justin said) but with two inserts. Just one row for each match (also with duplicate matchings as you said in the comment) and then all the rows from student. This can be done in the following way.

insert into studLoad ( studID, studName, <<timestamp column>> )
select studID, studName, systimestamp
from student
where studId in (
        selct studId
        from studLoad
    )
/
insert into studLoad (studID, studName)
select studID, studName
from student
/

And don't invert this execution order!!!

like image 142
Alessandro Rossi Avatar answered Feb 27 '26 20:02

Alessandro Rossi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!