|
Replies:
19
-
Pages:
2
[
1
2
| Next
]
-
Last Post:
Jul 24, 2008 7:27 AM
Last Post By: BluShadow
|
|
|
Posts:
58
Registered:
11/09/06
|
|
|
|
how to overcome mutating table error.
Posted:
Feb 12, 2007 2:59 AM
|
|
|
Hi,
I am writing a trigger on a table.In that trigger i want to query the same table for checking the row which i am going to insert exists or not by takeing rowcount.
What is the solution for this?
Thanks in advance,
Nilesh Malekar
|
|
|
Posts:
7,652
Registered:
02/20/01
|
|
|
|
Re: how to overcome mutating table error.
Posted:
Feb 12, 2007 3:09 AM
in response to: Ritesh2
|
|
|
Use packages.
Check out this link
|
|
|
Posts:
212
Registered:
10/17/05
|
|
|
|
Re: how to overcome mutating table error.
Posted:
Feb 12, 2007 3:18 AM
in response to: Ritesh2
|
|
|
|
u can even use Autonomus trigger to avoid Mutating table error.
U just have to add
Pragma Autonomous_transaction in declare
& include a commit.
Although this practice has its own drawbacks as the trigger will be independent
from the triggering event .Hence it may cause discrepency of data
|
|
|
Posts:
757
Registered:
09/07/98
|
|
|
|
Re: how to overcome mutating table error.
Posted:
Feb 12, 2007 3:58 AM
in response to: Ritesh2
|
|
|
|
Why can't you use a unique constraint?
Cheers,
Colin
|
|
|
Posts:
10,439
Registered:
08/27/03
|
|
|
|
Re: how to overcome mutating table error.
Posted:
Feb 12, 2007 4:17 AM
in response to: Colin 't Hart
|
|
|
Why can't you use a unique constraint?
Alternatively, depending on what action you want to happen if the row does exist, MERGE?
Mutating table errors are almost always an indicator of a sub-optimal design, so it is worth checking our requirements and assumptions to see if there is a different approach which will meet our requirements without causing a mutatting table error.
Cheers, APC
|
|
|
Posts:
1,536
Registered:
04/03/02
|
|
|
|
Re: how to overcome mutating table error.
Posted:
Feb 12, 2007 7:53 AM
in response to: Ritesh2
|
|
|
|
Using the AUTONOMOUS TRANSACTION pragma in the trigger's PL/SQL block is one way to got - probably the easiest - but has some considerations.
Under just the wrong conditions it can cause data issues. With luck you won't encounter these conditions, though. There are complete write-ups on this issue on-line, and particuarly in Tom Kyte's web site. In particular you need to know that if you do use ATs you can't see any uncommited changes on the trigger table
|
|
|
Posts:
336
Registered:
11/02/05
|
|
|
|
Re: how to overcome mutating table error.
Posted:
Feb 12, 2007 9:34 AM
in response to: Ritesh2
|
|
|
|
One possible solution to avoid make a count on this table for the id you want to insert if count returns 0 then insert else do not insert , This should not be handled inside the trigger but before making an Insert .
|
|
|
Posts:
10,439
Registered:
08/27/03
|
|
|
|
Re: how to overcome mutating table error.
Posted:
Feb 12, 2007 9:39 AM
in response to: Fantasy
|
|
|
This should not be handled inside the trigger but before making an Insert .
This will be no use in a multi-user environment.
Cheers, APC
|
|
|
Posts:
336
Registered:
11/02/05
|
|
|
|
Re: how to overcome mutating table error.
Posted:
Feb 12, 2007 9:42 AM
in response to: APC
|
|
|
|
Then Possibility deduces to Pragma Autonomous_transaction;
Thanx for opening the corridors (Multi User Environment)
|
|
|
Posts:
10,439
Registered:
08/27/03
|
|
|
|
Re: how to overcome mutating table error.
Posted:
Feb 12, 2007 9:48 AM
in response to: riedelme
|
|
|
you do use ATs you can't see any uncommited changes on the trigger table
The autonomous transaction approach is flawed in two ways. Yes it will not show you any uncommitted changes in other sessions, but that just results in ugly exceptions when we come to commit. More importantly it won't include any changes in out own session...
SQL> create or replace trigger trg1 before insert on t1
2 for each row
3 declare
4 n pls_integer;
5 pragma autonomous_transaction;
6 begin
7 select count(*) into n from t1
8 where col1 = :NEW.col1;
9 if n > 0 then raise_application_error(-20001, 'Problem!');
10 end if;
11 commit;
12 end;
13 /
Trigger created.
SQL> insert into t1 values (42, 'JJJJ')
2 /
1 row created.
SQL> insert into t1 values (42, 'JJJJ')
2 /
1 row created.
SQL> insert into t1 values (42, 'JJJJ')
2 /
1 row created.
SQL> select * from t1 where col1 = 42
2 /
COL1 COL2
----------
42 JJJJ
42 JJJJ
42 JJJJ
SQL> commit;
Commit complete.
SQL> delete from t1 where col1 = 42
2 /
3 rows deleted.
SQL> insert into t1 values (42, 'JJJJ')
2 /
insert into t1 values (42, 'JJJJ')
*
ERROR at line 1:
ORA-20001: Problem!
ORA-06512: at "A.TRG1", line 7
ORA-04088: error during execution of trigger 'A.TRG1'
SQL>
Cheers, APC
|
|
|
Posts:
10,439
Registered:
08/27/03
|
|
|
|
Re: how to overcome mutating table error.
Posted:
Feb 12, 2007 9:52 AM
in response to: Fantasy
|
|
|
Then Possibility deduces to Pragma Autonomous_transaction;
Nope, as I have just shown, the autonomous transaction approach doesn't work either. Depending on the precise requirenments the best solutions are:
(1) Primary or unique keys
(2) merge
(3) Redesign of data model or process flow
There is also
(4) serialization, e.g. with user defined locks, but we probably don't want to go there unless we really have to.
Cheers, APC
|
|
|
Posts:
336
Registered:
11/02/05
|
|
|
|
Re: how to overcome mutating table error.
Posted:
Feb 12, 2007 9:57 AM
in response to: APC
|
|
|
|
Create a function with Pragma Autonomous _Transaction and check for the newly inserted values by passing :new values frmo the trigger body to the Function if found then handle exceptions else insert into table based on this :new value passed.
|
|
|
Posts:
1,692
Registered:
02/03/99
|
|
|
|
Re: how to overcome mutating table error.
Posted:
Feb 12, 2007 10:01 AM
in response to: Ritesh2
|
|
|
|
|
|
Posts:
10,439
Registered:
08/27/03
|
|
|
|
Re: how to overcome mutating table error.
Posted:
Feb 12, 2007 10:05 AM
in response to: Fantasy
|
|
|
Create a function with Pragma Autonomous _Transaction
Fnord. What difference would that make? Precisely none. The whole point about autonomous transactions are that they are independent of the current transaction.
This does not just mean that our current transaction is isolated from commits or rollbacks in the autonomous transaction. It also means that any queries in the autonomous transaction cannot see the changes in the main transaction. Think of an autonomous transaction as opening up a new session: the exact same data consistency rules apply.
Cheers, APC
|
|
|
Posts:
10,439
Registered:
08/27/03
|
|
|
|
Re: how to overcome mutating table error.
Posted:
Feb 12, 2007 10:07 AM
in response to: Himanshu Kandpal
|
|
|
That link is broken. I think you meant to direct Ritesh to here.
Cheers, APC
|
|
|
|
Legend
|
|
Guru : 2500
- 1000000
pts
|
|
Expert : 1000
- 2499
pts
|
|
Pro : 500
- 999
pts
|
|
Journeyman : 200
- 499
pts
|
|
Newbie : 0
- 199
pts
|
|
Oracle ACE Director
|
|
Oracle ACE Member
|
|
Oracle Employee ACE
|
|
Helpful Answer
(5 pts)
|
|
Correct Answer
(10 pts)
|
|