473,394 Members | 1,831 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,394 software developers and data experts.

Mutuation Trigger

I am trying to update the changed date of a table. For instance, I have a table with 4 columns: column1, column2, column3, changed_dt.
If any of the first 3 columns get update, I want the changed date to update.

Expand|Select|Wrap|Line Numbers
  1.  
  2. CREATE TRIGGER MyTestTrigger
  3.     AFTER UPDATE of "column1", "column2", "column3"
  4.     ON Mytable REFERENCING OLD AS OLD NEW AS NEW
  5.     FOR EACH ROW
  6. begin
  7.  
  8. IF  UPDATING THEN
  9.                 UPDATE Mytable 
  10.     SET CHANGED_DT = SYSDATE;
  11. END IF;
  12.  
  13. END;
  14. /
  15.  

However, I am getting the mutuating error. Why?
Feb 29 '08 #1
2 1312
debasisdas
8,127 Expert 4TB
Please find more about mutating trigger here and here .
Mar 3 '08 #2
one of the best example to avoid mutation problem

Expand|Select|Wrap|Line Numbers
  1.  
  2. Create table CUG 
  3.  
  4. drop table CUG cascade constraints;
  5. create table CUG (
  6.   id_cug      number(12) not null primary key,
  7.   id_B        number(12) not null,
  8.   type        number(1),
  9. foreign key (id_B) references CUG (id_cug) 
  10. on delete cascade); 
  11.  
  12. --Next we create a temporary table to avoid the "Mutating Table Problem". 
  13.  
  14. drop table CUGTMP;
  15. create global temporary table CUGTMP (
  16.   id_B        number(12),
  17.   type        number(1))
  18. on commit delete rows; 
  19.  
  20. --The following trigger checks new rows (Inserts) in CUG 
  21.  
  22. create or replace trigger bi_r
  23. before insert on CUG
  24. for each row
  25. declare
  26.   l_type     CUG.type%type;
  27. begin
  28.   if (:new.type in (3,4)) then
  29.     select type into l_type from CUG
  30.      where id_cug = :new.id_B;
  31.   end if;
  32.   if (l_type != 2) then
  33.      raise_application_error(-20002,
  34.      'C and D CUGs must have a leading B');
  35.   end if;
  36. end;
  37.  
  38. --The following Trigger saves the new values for id_B in the temporary table. 
  39.  
  40. create or replace trigger au_r
  41. after update of id_B on CUG
  42. for each row
  43. begin
  44.   insert into CUGTMP (id_B,type)
  45.   values (:new.id_B,:new.type);
  46. end;
  47.  
  48. --The following Trigger finally checks, that C and D CUGs belong to a B CUG. 
  49.  
  50. create or replace trigger au_s
  51. after update of id_B on CUG
  52. declare
  53.   l_id_B        number(12);
  54.   l_typeCD      number(1);
  55.   l_typeB       number(1);
  56.   cursor cur_cugtmp is
  57.   select id_B,type
  58.    from CUGTMP;
  59. begin
  60.   open cur_cugtmp;
  61.   loop
  62.     fetch cur_cugtmp into l_id_B,l_typeCD;
  63.     exit when cur_cugtmp%notfound;
  64.      select type into l_typeB from CUG
  65.       where id_cug = l_id_B;
  66.     if (l_typeB != 2) then
  67.        raise_application_error(-20002,
  68.        'C and D CUGs must have a leading B');
  69.     end if;
  70.   end loop;
  71.   close cur_cugtmp;
  72. end;
  73.  
Test insert and update
Mar 5 '08 #3

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

Similar topics

1
by: Matik | last post by:
Hello to all, I have a small question. I call the SP outer the DB. The procedure deletes some record in table T1. The table T1 has a trigger after delete. This is very importand for me, that...
6
by: Scott CM | last post by:
I have a multi-part question regarding trigger performance. First of all, is there performance gain from issuing the following within a trigger: SELECT PrimaryKeyColumn FROM INSERTED opposed...
9
by: Martin | last post by:
Hello, I'm new with triggers and I can not find any good example on how to do the following: I have two tables WO and PM with the following fields: WO.WONUM, VARCHAR(10) WO.PMNUM,...
6
by: Mary | last post by:
We are developing a DB2 V7 z/OS application which uses a "trigger" table containing numerous triggers - each of which is activated by an UPDATE to a different column of this "trigger" table. When...
0
by: Dave Sisk | last post by:
I've created a system or external trigger on an AS/400 file a.k.a DB2 table. (Note this is an external trigger defined with the ADDPFTRG CL command, not a SQL trigger defined with the CREATE...
0
by: JohnO | last post by:
Thanks to Serge and MarkB for recent tips and suggestions. Ive rolled together a few stored procedures to assist with creating audit triggers automagically. Hope someone finds this as useful as...
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,...
7
by: Shane | last post by:
I have been instructed to write a trigger that effectively acts as a foreign key. The point (I think) is to get me used to writing triggers that dont use the primary key(s) I have created 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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.