Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Mutating Trigger

Posted on 2007-11-16
6
Medium Priority
?
1,139 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 28

Accepted Solution

by:
Naveen Kumar earned 500 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 74

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 74

Assisted Solution

by:sdstuber
sdstuber earned 500 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 74

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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

This post first appeared at Oracleinaction  (http://oracleinaction.com/undo-and-redo-in-oracle/)by Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…

618 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