Error with Delete Trigger [message #639875] |
Sun, 19 July 2015 21:26 |
|
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 #639923 is a reply to message #639888] |
Mon, 20 July 2015 07:13 |
|
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 #639925 is a reply to message #639924] |
Mon, 20 July 2015 07:29 |
|
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
|
|
|