Hi,
I have a table et_trial which has column trial_id, date_last_updated etc.
trial_id is the primary key and date_last_updated is a date field. I need to update date_last_updated whenever there is any update on this table. I wrote two types of triggers, but I still get table mutating error, please help me to trouble shoot this problem. The first type is:
CREATE OR REPLACE package et_trial_new_pkg
as
type array is table of et_trial%rowtype index by binary_integer;
newvals array;
empty array;
end;
/
CREATE OR REPLACE package et_trial_old_pkg
as
type array is table of et_trial%rowtype index by binary_integer;
oldvals array;
empty array;
end;
/
CREATE OR REPLACE TRIGGER et_trial_new_bu
BEFORE UPDATE of private_name ON et_trial
begin
et_trial_new_pkg.newvals := et_trial_new_pkg.empty;
end;
CREATE OR REPLACE TRIGGER et_trial_new_bufer
BEFORE UPDATE of private_name ON et_trial
for each row
declare
i number default et_trial_new_pkg.newvals.count+1;
begin
et_trial_new_pkg.newvals(i).private_name := :new.private_name ;
end;
CREATE OR REPLACE TRIGGER et_trial_old_bu
BEFORE UPDATE of private_name ON et_trial
begin
et_trial_old_pkg.oldvals := et_trial_old_pkg.empty;
end;
CREATE OR REPLACE TRIGGER et_trial_old_bufer
BEFORE UPDATE of private_name ON et_trial
for each row
declare
i number default et_trial_old_pkg.oldvals.count+1;
begin
et_trial_old_pkg.oldvals(i).private_name :=

ld.private_name;
end;
CREATE OR REPLACE TRIGGER trial_date_last_updated_t
AFTER UPDATE of private_name ON et_trial
FOR EACH ROW
begin
for i in 1 .. et_trial_new_pkg.newvals.count loop
IF et_trial_old_pkg.oldvals(i).private_name <> et_trial_new_pkg.newvals(i).private_name THEN
update et_trial set DATE_LAST_UPDATED = SYSDATE
where trial_id = et_trial_old_pkg.oldvals(i).trial_id;
END IF;
end loop;
end;
Then I wrote another type of triggers to solve the problem, but it's still not fixed:
create or replace package et_trial_pkg
as
type ridArray is table of rowid index by binary_integer;
newRows ridArray;
empty ridArray;
g_trigger_updating BOOLEAN := FALSE;
end;
/
create or replace trigger et_trial_bu
before update on et_trial
begin
if(g_trigger_updating = TRUE) then
et_trial_pkg.newRows := et_trial_pkg.empty;
end if;
end;
/
create or replace trigger et_trial_aufer
after update on et_trial for each row
begin
if(g_trigger_updating = TRUE) then
et_trial_pkg.newRows( et_trial_pkg.newRows.count+1 ) := :new.rowid;
end if;
end;
/
create or replace trigger et_trial_au
after update on et_trial
begin
et_trial_pkg.g_trigger_updating := TRUE;
for i in 1 .. et_trial_pkg.newRows.count loop
Update ET_Trial
set DATE_LAST_UPDATED = SYSDATE
where rowid = et_trial_pkg.newRows(i);
end loop;
et_trial_pkg.g_trigger_updating := FALSE;
end;
/
Any suggestions are welcome.