Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

mutating table error

519523May 30 2008 — edited May 30 2008

Hi,

I have created two tables. t_par is the parent table and t_child is the child table. I created two triggers, one on parent table and the other on the child table.That is, an after row trigger is created on the parent table - if the exp_date date on the parent table is not null then the same date must be updated in the child table too.
For the child table i have written a before row trigger where i need to check some effective and expiry date validations. Therefore i have to get values from the parent table and then compare them. For that i have a written a select query on the parent table in the child trigger. but it is causing mutating error. Please suggest the solution to handle this.

create table t_par(seq number, descr varchar2(20),eff_date date, exp_date date);

alter table t_par add constraint t_par_pk primary key(seq);

create table t_child(seq number, t_par_seq number,descr varchar2(20),eff_date date, exp_date date);

alter table t_child add constraint t_child foreign key(t_par_seq) references  t_par(seq);

/* Create a after row trigger on the parent table
*/
CREATE or REPLACE TRIGGER t_par_tar
AFTER UPDATE
on t_par
for each row
declare
begin
   update t_child 
   set exp_date = to_date(:new.exp_date,'mm/dd/yyyy')
   where exp_date IS NULL;
exception
when others
then
    dbms_output.put_line('Error in t_par_tar: '||sqlerrm);
end;
/

/* Create a before row trigger on the child table
*/
CREATE or REPLACE TRIGGER t_child_tbr
before UPDATE
on t_child
for each row
declare
  v_eff_date  DATE;
  v_exp_date  DATE;
  cursor c1 
  IS 
    select eff_date,
           exp_date
      from t_par
     where seq = :new.t_par_seq;
     
begin
   OPEN c1;
   FETCH c1 INTO v_eff_date, v_exp_date;
   CLOSE c1;   
   dbms_output.put_line('t_child tbr parent eff_date is '||v_eff_date ||' and exp_date is '||v_exp_date);
exception
when others
then
    dbms_output.put_line('Error in t_child_tbr: '||sqlerrm);
end;
/

Thanks in advance.

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 27 2008
Added on May 30 2008
1 comment
317 views