Solved

Mutating Trigger

Posted on 2007-11-16
6
1,133 Views
Last Modified: 2013-12-19
Hello Experts,

I have a prod_ins1 table with st_type,st_sn,wheel. st_type,st_sn are loaded from another table.
Based upon st_type wheel has to be populated. After wheel is populated it has to be loaded into 'wheel' column on Main_prod table.

CREATE OR REPLACE TRIGGER prod_ins1
   after INSERT
   ON prod_test1
 FOR EACH row

DECLARE
vno NUMBER;
BEGIN
update prod_test1 set wheel =
case when st_type = 'Symm ' then 'Wheel 1'
when st_type = 'Clar ' then 'Wheel 2'
when st_type like '%RAM%' then 'RAM'
end;
SELECT count(*) into vno from Main_Prod e  where e.st_sn = :new.st_sn;
if  vno > 0 then
update Main_prod  set
wheel =:new.wheel
where st_sn = :new.st_sn;
else
insert into Main_Prod(wheel)
values
(:new.wheel);
end if;
end;
=======================

ERROR at line 1:
ORA-04091: table MSUSER.PROD_TEST1 is mutating, trigger/function may not

ORA-04088: error during execution of trigger 'MSUSER.PROD_TEST1'

Can you please advise me??

Cheers
0
Comment
Question by:spsun
  • 3
  • 2
6 Comments
 
LVL 28

Accepted Solution

by:
Naveen Kumar earned 125 total points
ID: 20302947
You cannot update/insert/delete/select to the same table inside the trigger code on which the trigger is firing.

Try the below :

CREATE OR REPLACE TRIGGER prod_ins1
   before INSERT  -- changed after to before
   ON prod_test1
 FOR EACH row
DECLARE
vno NUMBER;
BEGIN
:new.wheel := case when :new.st_type = 'Symm ' then 'Wheel 1'
                   when :new.st_type = 'Clar ' then 'Wheel 2'
                   when :new.st_type like '%RAM%' then 'RAM'
              end;

SELECT count(*) into vno from Main_Prod e  where e.st_sn = :new.st_sn;
if  vno > 0 then
update Main_prod  set
wheel =:new.wheel
where st_sn = :new.st_sn;
else
insert into Main_Prod(wheel)
values
(:new.wheel);
end if;
end;
/

Thanks
0
 
LVL 73

Expert Comment

by:sdstuber
ID: 20303002
(if you don't really care how many things there are don't count them all,  count(*) is too expensive an operation if you don't really need it.  Unless, of course, you're counting a unique key.


However, since you're using 10g  (according to your Zones anyway) you can/should use the MERGE statement instead of count update/insert
0
 
LVL 73

Assisted Solution

by:sdstuber
sdstuber earned 125 total points
ID: 20303032
The merge version might look something like this...

When you have variables to merge instead of a query, just select null from dual as a dummy query.
Syntax requires a query so you have to put something there



CREATE OR REPLACE TRIGGER prod_ins1
    BEFORE INSERT
    ON prod_test1
    FOR EACH ROW
DECLARE
    vno   NUMBER;
BEGIN
    :NEW.wheel    :=
        CASE
            WHEN :NEW.st_type = 'Symm '
                THEN 'Wheel 1'
            WHEN :NEW.st_type = 'Clar '
                THEN 'Wheel 2'
            WHEN :NEW.st_type LIKE '%RAM%'
                THEN 'RAM'
        END;
    MERGE INTO main_prod mp
        USING (SELECT NULL
                 FROM DUAL) n
        ON (mp.st_sn = :NEW.st_sn)
        WHEN MATCHED THEN
            UPDATE
               SET mp.wheel = :NEW.wheel
        WHEN NOT MATCHED THEN
            INSERT(wheel)
            VALUES(:NEW.wheel);
END;
/
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 5

Expert Comment

by:sora
ID: 20310169
one of the ways to resolve mutating trigger problem is to have a combination of row level and statement level triggers. so at a BEFORE row level trigger populate a package variable(s) with the values you want to populate. Then in the AFTER statement level trigger read the package variable and populate the column to the table
0
 
LVL 73

Expert Comment

by:sdstuber
ID: 20312235
sora, you are correct,
 but in this case using before and after triggers is not necessary because there is no need to go back re-update the same rows.  It's just a matter of using the :new  identifier to allow the trigger to modify the current record
0
 
LVL 5

Expert Comment

by:sora
ID: 20317301
yes, correct, sdstuber. if the :new identifier can be used to modify the current record, and if thats ALL thats required, then we do not need a combo of row+statement level triggers
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to Create User-Defined Aggregates in Oracle Before we begin creating these things, what are user-defined aggregates?  They are a feature introduced in Oracle 9i that allows a developer to create his or her own functions like "SUM", "AVG", and…
I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question