473,386 Members | 1,820 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Help with Trigger Updating same row?

Hello all!

I have seen very wide discussions on this before on the web but I was wondering what your thoughts were.

I have a table where when a record is added, I want to update that row. Sounds simple. But I am getting the nasty error about mutating tables.

Here is a very generic summary.

TABLE_A
Field1 Number
Field2 Text

Insert into Table_A (Field1) Values (1000);

I have a trigger that I want to do the following.

Update Table_A Set Field2 = 'Changed Record' where Field1 = 1000;


That is a very rough design to spare the technical details but I am wondering what I need to do in order to update the same record that caused the trigger. I only want the trigger to fire on insert and not update/delete.

Thanks in advance for your time.
Chris
Feb 8 '08 #1
3 4032
Try This......................
Expand|Select|Wrap|Line Numbers
  1.  
  2. create table b2 (f1 number(3), f2 varchar2(10));
  3. //
  4. insert into b2 values(100,'abcd');
  5. //
  6.  create or replace trigger subtrig
  7.  before/after insert on b2
  8.  begin
  9.  update b2 set f2='wxyz' where f1=100;
  10.  end;
  11.  //
  12.  select * from b2 ;
  13. //
  14. insert into b2 values(200,'efgh');
  15. //
  16. select * from b2;
  17. //
  18.  
  19. note:- to avoid mutation problem use Temporary table solution and another one example to avoid mutation.
  20.  
  21. create or replace trigger  mutating before delete on emp1
  22.  for each row
  23.  begin
  24.  insert into emp1(empno,ename,job,sal)
  25.  values(1000,'raj','it',8000);
  26.  update emp1
  27.  set sal = 9000
  28.  where empno=2100;
  29.  end mutating;
  30.  /
  31.  
  32. SQL> delete from emp1
  33.   2  where empno=7846;
  34. delete from emp1
  35.             *
  36. ERROR at line 1:
  37. ORA-04091: table SCOTT.EMP1 is mutating, trigger/function may not see it
  38. ORA-06512: at "SCOTT.MUTATING", line 2
  39. ORA-04088: error during execution of trigger 'SCOTT.MUTATING'
  40. /
  41.  
  42. solution:-
  43.  
  44.  create or replace trigger  mutating before delete on emp1
  45. -- for each row
  46.  begin
  47.  insert into emp1(empno,ename,job,sal)
  48.  values(1000,'raj','it',8000);
  49.  update emp1
  50.  set sal = 9000
  51.  where empno=2100;
  52.  end mutating;
  53. /
  54. delete from emp1
  55. where empno=1000;
  56. /
  57.  
Feb 9 '08 #2
amitpatel66
2,367 Expert 2GB
subashsavji,

Can you make sure you will use [code] tags in future for any source code you post in this forum?

MODERATOR
Feb 10 '08 #3
Let me see if I have this straight.

- Put a trigger on my main table that inserts into a temporary table.

- Put a trigger on the temp table that does the update on the main table?

Do I have that right?
Feb 11 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Sean Utt | last post by:
Table "grps" Column | Type | Modifiers -------------+-----------------------------+-------------------------------- --------------------- grpsid ...
8
by: Jason | last post by:
I have a table that matches up Securities and Exchanges. Individual securities can belong on multiple exchanges. One of the columns, named PrimaryExchangeFlag, indicates if a particular exchange is...
3
by: Prince Kumar | last post by:
Hi All, I am trying to a get a trigger retrieved from Oracle to work on DB2 UDB 8.1. I am getting the following error when trying to create the trigger. How would I resolve this? create table...
18
by: Bill Smith | last post by:
The initial row is inserted with the colPartNum column containing a valid LIKE pattern, such as (without the single quotes) 'AB%DE'. I want to update the column value with the results of a query...
0
by: Bernard Cheung | last post by:
I am writing a trigger function. How can I know which fields are being updated in the PL/SQL function? For example I have a table here: CREATE TABLE COMPANY ( COMPANY_ID VARCHAR(10) NOT NULL,...
15
by: Jay | last post by:
I have a multi threaded VB.NET application (4 threads) that I use to send text messages to many, many employees via system.timer at a 5 second interval. Basically, I look in a SQL table (queue) to...
9
by: Ots | last post by:
I'm using SQL 2000, which is integrated with a VB.NET 2003 app. I have an Audit trigger that logs changes to tables. I want to apply this trigger to many different tables. It's the same trigger,...
0
by: KiranKGone | last post by:
Hello All, I need to define a trigger for updating the multiple columns of a target table when an insert happens on a subject table. I have written the following trigger, however, getting the...
11
by: tracy | last post by:
Hi, I really need help. I run this script and error message appeal as below: drop trigger log_errors_trig; drop trigger log_errors_trig ERROR at line 1: ORA04080: trigger 'LOG_ERRORS-TRIG'...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.