Return-Path: <root@fatcity.cts.com>
Received: from newsfeed.cts.com (newsfeed.cts.com [209.68.248.164])
 by naude.co.za (8.11.2/8.11.2) with SMTP id g7MK1dt30169
 for <oracle-l@naude.co.za>; Thu, 22 Aug 2002 16:01:39 -0400
Received: from fatcity.UUCP (uucp@localhost)
 by newsfeed.cts.com (8.9.3/8.9.3) with UUCP id MAA39238;
 Thu, 22 Aug 2002 12:58:59 -0700 (PDT)
Received: by fatcity.com (26-Feb-2001/v1.0g-b72/bab) via UUCP id 004BD7F6; Thu, 22 Aug 2002 12:56:13 -0800
Message-ID: <F001.004BD7F6.20020822125613@fatcity.com>
Date: Thu, 22 Aug 2002 12:56:13 -0800
To: Multiple recipients of list ORACLE-L <ORACLE-L@fatcity.com>
X-Comment: Oracle RDBMS Community Forum
X-Sender: Richard Huntley <rhuntley@mindleaders.com>
Sender: root@fatcity.com
Reply-To: ORACLE-L@fatcity.com
Errors-To: ML-ERRORS@fatcity.com
From: Richard Huntley <rhuntley@mindleaders.com>
Subject: RE: Avoding Mutation of Table trigger
Organization: Fat City Network Services, San Diego, California
X-ListServer: v1.0g, build 72; ListGuru (c) 1996-2001 Bruce A. Bergman
Precedence: bulk
Mime-Version: 1.0
Content-Type: multipart/mixed;	boundary="----=_NextPartTM-000-461cd080-b5e3-11d6-8b11-00b0d04949c1"
------=_NextPartTM-000-461cd080-b5e3-11d6-8b11-00b0d04949c1
Content-Type: multipart/alternative;
 boundary="----_=_NextPart_001_01C24A14.4527E900"
------_=_NextPart_001_01C24A14.4527E900
Content-Type: text/plain;
 charset="iso-8859-1"

Naba,

The solution for ORA-4091 is to create two triggers--a row level (before
insert or update) 
and a statement level (after insert or update).  In the row level you simply
record the values of :new.AMT
into a PL/SQL table.  Then in the statement level trigger you perform the
calculation based on the
recorded values in the PL/SQL table.  Record the values using a PL/SQL table
in a "package",
that way you won't have to worry about simultaneous updates by different
sessions.  

HTH,
Rich

-----Original Message-----
Sent: Tuesday, August 20, 2002 4:28 AM
To: Multiple recipients of list ORACLE-L


Hi,

    This problem/solution may be posted earlier, but would like to hear
    a solution from you.

I have two table  say TAB1 and TAB2. TAB2 is a detail of TAB1 table.

    1. TAB1
            Pk_key     numnber(10)
            AMT        numnber(14,2)
            ... More colums
       pk_key   is Primary Key.


    2. TAB2
            Pk_key     numnber(10)
            PK_sl        number(2)
            AMT        numnber(14,2)
            ... More colums
       1. pk_key + pk_sl  is Primary Key.
       2. pk_key is foreign key to tab1

Now I would like to write a trigger in TAB2 for INSERT and UPDATE operation
which will update the AMT coloum of TAB1. The value to be updated is sum of
AMT coloumn of TAB2 for the PK_KEY value. 
    i.e. select sum(amt) into tot from TAB2 where PK_KEY= :NEW.PK_KEY;

Since TAB2 is currently being modified it can not be read.

so, how to solve it ?

TIA

Regards.

Naba

-- 
Please see the official ORACLE-L FAQ: http://www.orafaq.com
-- 
Author: N J Neog
  INET: njneog@oil.asm.nic.in

Fat City Network Services    -- (858) 538-5051  FAX: (858) 538-5051
San Diego, California        -- Public Internet access / Mailing Lists
--------------------------------------------------------------------
To REMOVE yourself from this mailing list, send an E-Mail message
to: ListGuru@fatcity.com (note EXACT spelling of 'ListGuru') and in
the message BODY, include a line containing: UNSUB ORACLE-L
(or the name of mailing list you want to be removed from).  You may
also send the HELP command for other information (like subscribing).

------_=_NextPart_001_01C24A14.4527E900
Content-Type: text/html;
 charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV=3D"Content-Type" CONTENT=3D"text/html; =
charset=3Diso-8859-1">
<META NAME=3D"Generator" CONTENT=3D"MS Exchange Server version =
5.5.2653.12">
<TITLE>RE: Avoding Mutation of Table trigger</TITLE>
</HEAD>
<BODY>

<P><FONT SIZE=3D2>Naba,</FONT>
</P>

<P><FONT SIZE=3D2>The solution for ORA-4091 is to create two =
triggers--a row level (before insert or update) </FONT>
<BR><FONT SIZE=3D2>and a statement level (after insert or =
update).&nbsp; In the row level you simply record the values of =
:new.AMT</FONT>
<BR><FONT SIZE=3D2>into a PL/SQL table.&nbsp; Then in the statement =
level trigger you perform the calculation based on the</FONT>
<BR><FONT SIZE=3D2>recorded values in the PL/SQL table.&nbsp; Record =
the values using a PL/SQL table in a &quot;package&quot;,</FONT>
<BR><FONT SIZE=3D2>that way you won't have to worry about simultaneous =
updates by different sessions.&nbsp; </FONT>
</P>

<P><FONT SIZE=3D2>HTH,</FONT>
<BR><FONT SIZE=3D2>Rich</FONT>
</P>

<P><FONT SIZE=3D2>-----Original Message-----</FONT>
<BR><FONT SIZE=3D2>From: njneog@oil.asm.nic.in [<A =
HREF=3D"mailto:njneog@oil.asm.nic.in">mailto:njneog@oil.asm.nic.in</A>]<=
/FONT>
<BR><FONT SIZE=3D2>Sent: Tuesday, August 20, 2002 4:28 AM</FONT>
<BR><FONT SIZE=3D2>To: Multiple recipients of list ORACLE-L</FONT>
<BR><FONT SIZE=3D2>Subject: Avoding Mutation of Table trigger</FONT>
</P>
<BR>

<P><FONT SIZE=3D2>Hi,</FONT>
</P>

<P><FONT SIZE=3D2>&nbsp;&nbsp;&nbsp; This problem/solution may be =
posted earlier, but would like to hear</FONT>
<BR><FONT SIZE=3D2>&nbsp;&nbsp;&nbsp; a solution from you.</FONT>
</P>

<P><FONT SIZE=3D2>I have two table&nbsp; say TAB1 and TAB2. TAB2 is a =
detail of TAB1 table.</FONT>
</P>

<P><FONT SIZE=3D2>&nbsp;&nbsp;&nbsp; 1. TAB1</FONT>
<BR><FONT =
SIZE=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp; Pk_key&nbsp;&nbsp;&nbsp;&nbsp; numnber(10)</FONT>
<BR><FONT =
SIZE=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp; AMT&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; numnber(14,2)</FONT>
<BR><FONT =
SIZE=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp; ... More colums</FONT>
<BR><FONT SIZE=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
pk_key&nbsp;&nbsp; is Primary Key.</FONT>
</P>
<BR>

<P><FONT SIZE=3D2>&nbsp;&nbsp;&nbsp; 2. TAB2</FONT>
<BR><FONT =
SIZE=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp; Pk_key&nbsp;&nbsp;&nbsp;&nbsp; numnber(10)</FONT>
<BR><FONT =
SIZE=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp; PK_sl&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; number(2)</FONT>
<BR><FONT =
SIZE=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp; AMT&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; numnber(14,2)</FONT>
<BR><FONT =
SIZE=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp; ... More colums</FONT>
<BR><FONT SIZE=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1. pk_key + =
pk_sl&nbsp; is Primary Key.</FONT>
<BR><FONT SIZE=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2. pk_key is =
foreign key to tab1</FONT>
</P>

<P><FONT SIZE=3D2>Now I would like to write a trigger in TAB2 for =
INSERT and UPDATE operation</FONT>
<BR><FONT SIZE=3D2>which will update the AMT coloum of TAB1. The value =
to be updated is sum of</FONT>
<BR><FONT SIZE=3D2>AMT coloumn of TAB2 for the PK_KEY value. </FONT>
<BR><FONT SIZE=3D2>&nbsp;&nbsp;&nbsp; i.e. select sum(amt) into tot =
from TAB2 where PK_KEY=3D :NEW.PK_KEY;</FONT>
</P>

<P><FONT SIZE=3D2>Since TAB2 is currently being modified it can not be =
read.</FONT>
</P>

<P><FONT SIZE=3D2>so, how to solve it ?</FONT>
</P>

<P><FONT SIZE=3D2>TIA</FONT>
</P>

<P><FONT SIZE=3D2>Regards.</FONT>
</P>

<P><FONT SIZE=3D2>Naba</FONT>
</P>

<P><FONT SIZE=3D2>-- </FONT>
<BR><FONT SIZE=3D2>Please see the official ORACLE-L FAQ: <A =
HREF=3D"http://www.orafaq.com" =
TARGET=3D"_blank">http://www.orafaq.com</A></FONT>
<BR><FONT SIZE=3D2>-- </FONT>
<BR><FONT SIZE=3D2>Author: N J Neog</FONT>
<BR><FONT SIZE=3D2>&nbsp; INET: njneog@oil.asm.nic.in</FONT>
</P>

<P><FONT SIZE=3D2>Fat City Network Services&nbsp;&nbsp;&nbsp; -- (858) =
538-5051&nbsp; FAX: (858) 538-5051</FONT>
<BR><FONT SIZE=3D2>San Diego, =
California&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -- Public Internet =
access / Mailing Lists</FONT>
<BR><FONT =
SIZE=3D2>---------------------------------------------------------------=
-----</FONT>
<BR><FONT SIZE=3D2>To REMOVE yourself from this mailing list, send an =
E-Mail message</FONT>
<BR><FONT SIZE=3D2>to: ListGuru@fatcity.com (note EXACT spelling of =
'ListGuru') and in</FONT>
<BR><FONT SIZE=3D2>the message BODY, include a line containing: UNSUB =
ORACLE-L</FONT>
<BR><FONT SIZE=3D2>(or the name of mailing list you want to be removed =
from).&nbsp; You may</FONT>
<BR><FONT SIZE=3D2>also send the HELP command for other information =
(like subscribing).</FONT>
</P>

</BODY>
</HTML>
------_=_NextPart_001_01C24A14.4527E900--

------=_NextPartTM-000-461cd080-b5e3-11d6-8b11-00b0d04949c1--

-- 
Please see the official ORACLE-L FAQ: http://www.orafaq.com
-- 
Author: Richard Huntley
  INET: rhuntley@mindleaders.com

Fat City Network Services    -- (858) 538-5051  FAX: (858) 538-5051
San Diego, California        -- Public Internet access / Mailing Lists
--------------------------------------------------------------------
To REMOVE yourself from this mailing list, send an E-Mail message
to: ListGuru@fatcity.com (note EXACT spelling of 'ListGuru') and in
the message BODY, include a line containing: UNSUB ORACLE-L
(or the name of mailing list you want to be removed from).  You may
also send the HELP command for other information (like subscribing).

