Home » SQL & PL/SQL » SQL & PL/SQL » Error with Delete Trigger (Oracle)
Error with Delete Trigger [message #639875] Sun, 19 July 2015 21:26 Go to next message
mattfriend
Messages: 12
Registered: September 2014
Junior Member
Hi Folks:

I have tried using the following trigger. It inserts ok, updates, ok, but gives a mutating table error when I try to delete from the HARVEST_DATA table..

The trigger fires when an insert, update, or delete occurs.

Table the Trigger is attached to: HARVEST_DATA

Table that receives the input of the trigger (the insert, update, delete): BALES_STORAGE

The Trigger Code I am using is:

CREATE OR REPLACE TRIGGER FARM.HARVEST_DATA_TRG
AFTER DELETE OR INSERT OR UPDATE
ON FARM.BALES_HARVESTED
REFERENCING NEW AS New OLD AS Old
FOR EACH ROW
BEGIN

  IF INSERTING THEN
             insert into BALES_STORAGE (BALES_RECEIVED_GAINED, CROP, CUTTING, STACKYARD, DATE_MOVED, HARVEST_ID, BALE_YEAR) values(:new.BALES_RECEIVED, :new.CROP, :new.CUTTING, :new.STACKYARD, :new.DATE_RECEIVED, :new.HARVEST_ID, :new.BALE_YEAR);
             
      ELSIF UPDATING THEN
              UPDATE BALES_STORAGE 
              SET  BALES_RECEIVED_GAINED= :new.BALES_RECEIVED, 
                   CROP= :new.CROP, 
                   CUTTING= :new.CUTTING, 
                   STACKYARD= :new.STACKYARD,
                   DATE_MOVED= :new.DATE_RECEIVED, 
                   HARVEST_ID= :new.HARVEST_ID,
                   BALE_YEAR= :new.BALE_YEAR 
               WHERE  BALES_STORAGE.harvest_id = :old.harvest_id;
                 
       ELSIF DELETING THEN
                   DELETE FROM BALES_STORAGE
                   WHERE  harvest_id = :old.harvest_id;    
       END IF;
END;
/

I have attached the error message.

Thanks,

Matthew
Re: Error with Delete Trigger [message #639876 is a reply to message #639875] Sun, 19 July 2015 21:34 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
>ON FARM.BALES_HARVESTED
why does above differ from below?
>Table the Trigger is attached to: HARVEST_DATA

>I try to delete from the HARVEST_DATA table..
why does above differ from below?
>DELETE FROM BALES_STORAGE

It appears we can only trust half of what you post, but don't know which half is TRUE.
Re: Error with Delete Trigger [message #639877 is a reply to message #639876] Sun, 19 July 2015 22:07 Go to previous messageGo to next message
mattfriend
Messages: 12
Registered: September 2014
Junior Member
Sorry, I was thinking of what i called the trigger when I named .

The table the trigger is attached to is BALES_HARVESTED
The inserts, updates, and deletes are performed on BALES_STORAGE

Matthew
Re: Error with Delete Trigger [message #639878 is a reply to message #639877] Sun, 19 July 2015 22:28 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
post trigger code on BALES_STORAGE table.
Re: Error with Delete Trigger [message #639888 is a reply to message #639878] Mon, 20 July 2015 03:26 Go to previous messageGo to next message
cookiemonster
Messages: 13922
Registered: September 2008
Location: Rainy Manchester
Senior Member
To be clear - that trigger on it's own can't cause mutating table, so there has to be another trigger involved - presumably a delete one on bales_storage.
Re: Error with Delete Trigger [message #639923 is a reply to message #639888] Mon, 20 July 2015 07:13 Go to previous messageGo to next message
mattfriend
Messages: 12
Registered: September 2014
Junior Member
Hi BlackSwan and Cookiemonster:
Thank you for helping me.

The only trigger that is on the BALES_STORAGE table is for the Primary Key:

CREATE OR REPLACE TRIGGER FARM.TRIG_BALES_STORAGE_PK BEFORE INSERT ON FARM.BALES_STORAGE REFERENCING OLD AS "OLD" NEW AS "NEW" FOR EACH ROW
begin  
   if inserting then 
      if :NEW."STORAGE_ID" is null then 
         select SEQ_STORAGE_ID.nextval into :NEW."STORAGE_ID" from dual; 
        end if; 
   end if; 
end;
/


The trigger on the BALES_HARVEST table I posted yesterday compiles ok and executes/works ok for insert and update statements just not delete.
Thanks again,
Matthew

[Updated on: Mon, 20 July 2015 07:15]

Report message to a moderator

Re: Error with Delete Trigger [message #639924 is a reply to message #639923] Mon, 20 July 2015 07:15 Go to previous messageGo to next message
cookiemonster
Messages: 13922
Registered: September 2008
Location: Rainy Manchester
Senior Member
Is there a foreign key between the two tables?
Re: Error with Delete Trigger [message #639925 is a reply to message #639924] Mon, 20 July 2015 07:29 Go to previous message
mattfriend
Messages: 12
Registered: September 2014
Junior Member
Thanks for the help Cookiemonster and Blackswan!

It is working now. The two tables were foreign keyed using the harvest_id. When you mentioned the foreign key I looked and it was set to cascade on delete. Once I removed the cascade it fires successfully now!

Thanks!
Matthew

[Updated on: Mon, 20 July 2015 07:49]

Report message to a moderator

Previous Topic: How to Split the Records
Next Topic: linking gv$sql and gv$session
Goto Forum:
  


Current Time: Fri May 10 13:55:47 CDT 2024