MERGE fungerer perfekt med Native dynamisk SQL (EXECUTE IMMEDIATE):
create table so_test(pk number not null primary key, value varchar2(20));
insert into so_test(pk, value) values(1, 'one');
declare
l_SQL varchar2(4000);
l_tablename varchar2(4000) default 'so_test';
begin
l_SQL := 'merge into ' || l_tablename || ' target' ||
' using (select 1 pk, ''eins'' value from dual union all
select 2 pk, ''zwei'' value from dual) source
on (target.pk = source.pk)
when matched then
update set target.value = source.value
when not matched then
insert values(source.pk, source.value)
';
dbms_output.put_line(l_sql);
execute immediate l_SQL;
end;
Kan du venligst sende den fejlmeddelelse, du får, når du bruger MERGE?