Trigger update joint view with trigger table - oracle

I'm trying to get my trigger to update a view that joins two table one of which is the table that activates the trigger causing this error
INSERT INTO MC_CLUB_MBR_TRANSACTION
*
ERROR at line 1:
ORA-04091: table ESP8339.MC_CLUB_MBR_TRANSACTION is mutating, trigger/function may not see it
ORA-06512: at "ESP8339.TRG_MC_UPD_DUES", line 2
ORA-04088: error during execution of trigger 'ESP8339.TRG_MC_UPD_DUES'
Here is the code I'm using
CREATE OR REPLACE TRIGGER TRG_MC_UPD_DUES
AFTER UPDATE OR INSERT ON
MC_CLUB_MBR_TRANSACTION
FOR EACH ROW
BEGIN
UPDATE MC_TRAN_V
SET MC_TRAN_V.DUES_DUE = (MC_TRAN_V.ANNUAL_DUES -
MC_TRAN_V.DUES_PAID_TO_DATE);
END;
/
INSERT INTO MC_CLUB_MBR_TRANSACTION
(
CLUB_CODE,
STUDENT_ID,
DUES_PAID_TO_DATE
)
VALUES
(
777,
20010,
300
);

Related

I got these error after trying to insert a row in a table and have created a trigger

i'm getting these error:
INSERT INTO "DANIEL"."STATION" (NUM_STATION, NOM_STATION, ALTITUDE, REGION)
VALUES ('6', 'Baalbak', '1250', 'Baalbek')
ORA-04091: table DANIEL.STATION is mutating, trigger/function may not see it
ORA-06512: at "DANIEL.TRUPDATE_HIST_STATION", line 5
ORA-04088: error during execution of trigger 'DANIEL.TRUPDATE_HIST_STATION'
ORA-06512: at line 1
One error saving changes to table "DANIEL"."STATION":
Row 6: ORA-04091: table DANIEL.STATION is mutating, trigger/function may not
see it
ORA-06512: at "DANIEL.TRUPDATE_HIST_STATION", line 5
ORA-04088: error during execution of trigger 'DANIEL.TRUPDATE_HIST_STATION'
ORA-06512: at line 1
after inserting a row in table station
And here is the code of the trigger that i just wrote:
create or replace TRIGGER trupdate_hist_station
AFTER INSERT ON STATION
FOR EACH ROW
DECLARE
V_num_station NUMBER;
BEGIN
BEGIN
SELECT s.NUM_STATION
INTO v_num_station
FROM STATION s
WHERE s.NUM_STATION = :NEW.NUM_STATION;
EXCEPTION
WHEN no_data_found THEN
v_num_station :=0;
END;
INSERT INTO HIST_STATION
("NUM_STATION","ANNEE","NB_RESERV","REVENU")
VALUES
(V_num_station,(SELECT to_char(SYSDATE,'YYYY')FROM DUAL),0,0);
END trupdate_hist_station;
i just need to add a new row in Hist_table after adding a row in table Station
No need to SELECT anything from the table you're inserting into (that's what causes mutation).
Also, get rid of double quotes, they aren't necessary as column names are defaulted to uppercase anyway.
Furthermore, don't SELECT year from dual - extract it from SYSDATE.
This should do:
create or replace TRIGGER trupdate_hist_station
AFTER INSERT ON STATION
FOR EACH ROW
BEGIN
INSERT INTO HIST_STATION
(num_station, annee, nb_reserv, revenu)
VALUES
(:new.num_station, extract (year from sysdate), 0, 0);
END trupdate_hist_station;

SQL Error: ORA-04091 while executing Trigger

I am trying to execute my trigger:
CREATE OR REPLACE TRIGGER Trg_video_bfr_delete
AFTER DELETE ON CMS_VIDEO
FOR EACH ROW
DECLARE
CODE varchar2(60);
BEGIN
SELECT CODE INTO CODE
from CMS_VIDEO
WHERE CODE = :OLD.CODE;
IF CODE IS NOT NULL THEN
INSERT INTO ASSET_DELETE_INDEX(CODE,ASSET_TYPE,IW_VPATH,LANGUAGE,MODIFIED_DTE)
VALUES (CODE,'Video',:old.IW_VPATH,:old.CONTENT_LANGUAGE,sysdate);
END IF;
EXCEPTION
WHEN NO_DATA_FOUND THEN
INSERT INTO ASSET_DELETE_INDEX (CODE,ASSET_TYPE,IW_VPATH,LANGUAGE,MODIFIED_DTE)
VALUES (CODE,'Video',null,:old.CONTENT_LANGUAGE,sysdate);
COMMIT;
END Trg_video_bfr_delete;
/
But I am getting the following error while executing a delete command on the table
Error report -
SQL Error: ORA-04091: table LSDS.CMS_VIDEO is mutating, trigger/function may not see it
ORA-06512: at "LSDS.TRG_VIDEO_BFR_DELETE", line 6
ORA-04088: error during execution of trigger 'LSDS.TRG_VIDEO_BFR_DELETE'
04091. 00000 - "table %s.%s is mutating, trigger/function may not see it"
*Cause: A trigger (or a user defined plsql function that is referenced in
this statement) attempted to look at (or modify) a table that was
in the middle of being modified by the statement which fired it.
*Action: Rewrite the trigger (or function) so it does not read that table.
Could anyone please help?
You have written trigger AFTER DELETE ON CMS_VIDEO. This trigger will fire when DELETE is performed from CMS_VIDEO table. So when CMS_VIDEO table is being modified, you cannot modify or query the same table in any of your triggers, procedures or functions.
CREATE OR REPLACE TRIGGER Trg_video_bfr_delete
AFTER DELETE ON CMS_VIDEO
FOR EACH ROW
BEGIN
IF :old.CODE IS NOT NULL THEN
INSERT INTO ASSET_DELETE_INDEX(CODE, ASSET_TYPE, IW_VPATH, LANGUAGE, MODIFIED_DTE)
VALUES (:old.CODE, 'Video', :old.IW_VPATH, :old.CONTENT_LANGUAGE, sysdate);
END IF;
END Trg_video_bfr_delete;
/
This is because you are deleting record from table and same table is used as reference inside trigger. This is case of "Mutating Error". You should switch to statement level trigger is it fits your requirement or you can use compound trigger which has multiple entry point and are capable of handling such scenarios.
Please refer https://oracle-base.com/articles/9i/mutating-table-exceptions
As per the snippet provided, I dont see any point in doing a SELECT on mutating TABLE.
We can easily ise :OLD.CODE to fetch the value. Hope below snippet helps.
CREATE OR REPLACE TRIGGER Trg_video_bfr_delete AFTER
DELETE ON CMS_VIDEO FOR EACH ROW
-- DECLARE CODE VARCHAR2(60);
BEGIN
-- SELECT CODE INTO CODE FROM CMS_VIDEO WHERE CODE = :OLD.CODE;
IF CODE IS NOT NULL THEN
INSERT
INTO ASSET_DELETE_INDEX
(
CODE,
ASSET_TYPE,
IW_VPATH,
LANGUAGE,
MODIFIED_DTE
)
VALUES
(
:old.code,
'Video',
:old.IW_VPATH,
:old.CONTENT_LANGUAGE,
sysdate
);
END IF;
EXCEPTION
WHEN OTHERS THEN
INSERT
INTO ASSET_DELETE_INDEX
(
CODE,
ASSET_TYPE,
IW_VPATH,
LANGUAGE,
MODIFIED_DTE
)
VALUES
(
:old.code,
'Video',
NULL,
:old.CONTENT_LANGUAGE,
sysdate
);
COMMIT;
END Trg_video_bfr_delete;

Insert inside the trigger in oracle [duplicate]

This question already has answers here:
Oracle trigger after insert or delete
(3 answers)
Closed 5 years ago.
Im trying to make simple trigger for students project. It will be firing on the prices table.
CREATE TABLE "STD_USER"."HT_PRICES"
( "PRC_ID" NUMBER,
"PRC_DATE_FROM" DATE,
"PRC_DATE_TO" DATE,
"PRC_STD_ID" NUMBER,
"PRC_AMOUNT" NUMBER
)
The main goal of the trigger is to checking if any reservation do not have any reference to updated Prices row.
If any row actually have reference on updated row we will need to insert new price into the prices table with starting date (PRC_DATE_FROM) equal to the end of the last reservation.
Trigger body:
create or replace
TRIGGER "TRG_NEW_PRICE" BEFORE UPDATE ON HT_PRICES
REFERENCING OLD AS old NEW AS new
FOR EACH ROW
DECLARE
v_last_rsv_date DATE;
v_temp_date_to DATE;
v_std_id NUMBER;
v_amount NUMBER;
BEGIN
BEGIN
SELECT MAX(RSV_DATE_TO)
INTO v_last_rsv_date
FROM HT_RESERVATION,
HT_ROOMS,
HT_STANDARDS,
HT_PRICES
WHERE RSV_ROM_ID=ROM_ID
AND ROM_STD_ID=STD_ID
AND STD_ID=:new.PRC_STD_ID;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RETURN;
END;
--do skrocenia starej ceny
v_temp_date_to:= :new.PRC_DATE_TO;
--poprawna data dla nowej ceny to v_last_rsv_date
:new.PRC_DATE_TO:= v_last_rsv_date;
v_amount:=:new.PRC_AMOUNT;
:new.PRC_AMOUNT:=:old.PRC_AMOUNT;
v_std_id:=:new.PRC_STD_ID;
:new.PRC_STD_ID:=:old.PRC_STD_ID;
COMMIT;
--skrocenie starej ceny w nowym rekordzie cenowym
HP_MANAGING.ADD_PRICE(v_last_rsv_date, v_temp_date_to, v_std_id, v_amount);
END;
Any ideas how to fix it? When I'm trying to test it I'm getting logs:
Error starting at line 10 in command:
update HT_PRICES SET PRC_AMOUNT=501 where prc_id=1
Error report:
SQL Error: ORA-04091: table STD_USER.HT_PRICES is mutating, trigger/function may not see it
ORA-06512: at "STD_USER.TRG_NEW_PRICE", line 8
ORA-04088: error during execution of trigger 'STD_USER.TRG_NEW_PRICE'
04091. 00000 - "table %s.%s is mutating, trigger/function may not see it"
*Cause: A trigger (or a user defined plsql function that is referenced in
this statement) attempted to look at (or modify) a table that was
in the middle of being modified by the statement which fired it.
*Action: Rewrite the trigger (or function) so it does not read that table.
1) Place your SELECT into a local procedure marked with pragma autonomous_transaction, this will allow select to run, though you won't see your current transaction uncommitted changes.
2) What is the reason to have COMMIT; in a BEFORE DML trigger?

How to solve the Oracle DB trigger error?

I wrote a DB trigger to monitor an insert action. After inserting a new record, I would like to automatically set the CREATION_DATE to sysdate.
I get an error when I want to insert a new record:
error
ORA-04091: table REPORT is mutating, trigger/function may not
see it
ORA-06512: at "CREATION_DATE_TEST", line 2
ORA-04088: error during execution of trigger 'CREATION_DATE_TEST'
My code:
CREATE OR REPLACE TRIGGER creation_date_test
AFTER INSERT ON REPORT FOR EACH ROW
BEGIN
UPDATE REPORT set CREATION_DATE = sysdate
WHERE ROWID = :new.ROWID;
END;
I also tried to replace ROWID = :new.ROWID with PROJECT_ID = new.PROJECT_ID. It throws the same error.
It sounds like you just want a before insert trigger that sets the :new.creation_date
create or replace trigger creation_date_test
before insert on report
for each row
begin
:new.creation_date := sysdate;
end;

ORA-04091: table is mutating, trigger/function may not see it ORA-06512

I created a trigger to insert a log in a table when an insert is made on another table.
This is the code of the trigger:
CREATE OR REPLACE TRIGGER scheme1.Inserting_Feed
AFTER INSERT ON scheme2.payments FOR EACH ROW
BEGIN
INSERT INTO scheme2.db-logger(ID, TECHNOLOGY, WORKFLOW, NAME_EVENT, TIME_EVENT)
VALUES(:NEW.id,'Repository','UP',(select repo.name
from scheme1.repository repo
join scheme2.payments pay
on repo.id = pay.repository_id
where repo.id = NEW.repository_id), SYSDATE);
END;
I try to run this trigger but when I make an insert on the table payments I get following error:
Error MT101-GENERAL_LOAD_ERROR: java.sql.SQLSyntaxErrorException:
ORA-04091: table scheme1.payments is mutating, trigger/function may
not see it ORA-06512: at "scheme1.Inserting_Feed", line 2 ORA-04088:
error during execution of trigger 'scheme1.Inserting_Feed'
What I understand is that the error says the table is changing, but the trigger doesn't see it. How does this come?
Why join to the scheme2.payments table in the subquery in the insert statement? Couldn't you just do:
CREATE OR REPLACE TRIGGER scheme1.inserting_feed
AFTER INSERT
ON scheme2.payments
FOR EACH ROW
BEGIN
INSERT INTO scheme2.db_logger (id,
technology,
workflow,
name_event,
time_event)
VALUES ( :new.id,
'Repository',
'UP',
(SELECT repo.name
FROM scheme1.repository repo
WHERE repo.id = :new.repository_id),
SYSDATE);
END;
/

Resources