Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Merge not working as expected

Munna NJan 6 2025

HI All,

I am using Oracle 19c version and facing doing merge with collections and union.Below is the code snippet and expected outcome

CREATE TABLE projects_dest ( SNO NUMBER PRIMARY KEY, project_name VARCHAR2(100), in_date DATE DEFAULT SYSDATE, out_date DATE DEFAULT NULL );
INSERT INTO projects_dest(SNO, project_name) VALUES (1, 'Project Alpha'); 
INSERT INTO projects_dest(SNO, project_name) VALUES (2, 'Project Beta'); 
INSERT INTO projects_dest(SNO, project_name) VALUES (3, 'Project Gamma');

Now I am reading the data from source and want to merge data based on SNO. If any new record insert and for any missing record update out_date to sysdate.

declare
cursor c_pop_project is
with src_data as(
select 2 as SNO, ‘Project Beta’ as Project_name from dual
union
select 3 as SNO, ‘Project Gamma’ as Project_name from dual
union
select 4 as SNO, ‘Project X’ as Project_name from dual)
select SNO,Project_name from src_data ;
type type_tbl_project is table of c_pop_project%rowtype;
load_tbl_project type_tbl_project;
begin
open c_pop_project
loop
fetch c_pop_project bulk collect into load_tbl_project limit 50000;
exit when load_tbl_project.count =0;
forall load_tbl_project.fisrt..load_tbl_project.last save exceptions
merge into projetcs tgt using
(select load_tbl_project.sno as sno,
        load_tbl_project.project_name as project_name
        'NEW' ins_upd_ind
        from dual
        minus
        select sno ,project_name , 'NEW' from projects
        union
        select sno ,project_name , 'SOFT_DEL' ins_upd_ind from projects
        minus
        select load_tbl_project.sno as sno,
load_tbl_project.project_name as project_name
'SOFT_DEL' ins_upd_ind
from dual
)src on (src.sno = tgt.sno and ins_upd_ind = 'SOFT_DEL' )
when matched then
update out_date = sysdate
when not matched then
insert 
(sno,
project_name
)
values
(src.sno,
src.project_name
);
commit;
end loop;
close c_pop_project;
exception
when others then
raise;
end;

This is the similar code snippet I have , when I run this this is updating all existing records out_date to sysdate instead of SNO1 record.

Can anyone please check this and let me know where is the issue in this code logic.

Comments
Post Details
Added on Jan 6 2025
12 comments
150 views