Drop test table
declare
no_table exception;
pragma Exception_Init (no_table, -00942);
begin
execute immediate 'drop table ero_tst';
exception
when no_table
then
null;
end;
Statement processed.
Create test table
create table ero_tst
as
select level id
, cast (null as number(3,0)) mod_count
from dual
connect by level <= 10
Table created.
merge that accesses the table to be manipulated
declare
stmnt clob;
begin
stmnt := 'merge /*+ WITH_PLSQL */
into ero_tst tgt
using (select id
, id * 2 mod_count
from ero_tst
) src
on (tgt.id = src.id
)
when matched
then
update
set tgt.mod_count = src.mod_count
';
execute immediate stmnt;
end;
/
Show result of merge
select *
from ero_tst
order by id ;
Merge statement causing mutating table
declare
stmnt clob;
begin
stmnt := 'merge /*+ WITH_PLSQL */
into ero_tst tgt
using (
with
function mod_x_count
(id_value in number
)
return number
is
return_value number;
begin
-- find the number of ids that are divisible by this id
select count(*)
into return_value
from ero_tst
where mod(id, id_value) = 0
;
return(return_value);
end mod_x_count;
--
select id
, mod_x_count(id) mod_count
from ero_tst
) src
on (tgt.id = src.id
)
when matched
then
update
set tgt.mod_count = src.mod_count
';
execute immediate stmnt;
end;
ORA-04091: table SQL_MHVQFCWBUVTVRMLMRCRKPNSIT.ERO_TST is mutating, trigger/function may not see it
Show result of merge
select *
from ero_tst
order by id ;
ID | MOD_COUNT | 1 | - | 2 | - | 3 | - | 4 | - | 5 | - | 6 | - | 7 | - | 8 | - | 9 | - | 10 | - |
---|