I have a trigger set to fire after an update of a specific column. I want to update the value of a specific column after a specific value from another column changes to another specific value.
I'm getting the errors:
ORA-04091: table tableName is mutating, trigger/function may not see it
ORA-06512: at "triggerName", line 14
ORA-04088: error during execution of trigger 'triggerName'
I've rewritten this as a before update and after update, as well as tried storing the logic in a function, and using "pragma autonomous_transaction", but the error still gets thrown.
BEFORE update of columnName1 ON tableName
FOR EACH ROW
BEGIN
if :new.columnName1 = 3 AND :old.columnName1 = 1 then
update tablename
set columnName2= MOD(sequenceName.NextVal, 5) + 1
where tableName.columnName2 = :old.columnName2;
end if;
END;
/
I don't understand why the entire table is labelled as "mutating" when I am not updating the column affected by the update the trigger is responding to. Surely, you should be able to update the value of a column in a table if another value in the table changes, or am I crazy here?
Note: Only one entry would be affected at a time. In my application logic, I update the status of some person in the database. I want the database to do some logic only on a specific status change, and I want to avoid using API calls for the logic here, as you can see, it is simply one line of logic in PLSQL.
Thanks
Perhaps you don't really want to update the table but just change a value in the specific row being updated.
If so, eschew the update and just set the value:
BEFORE update of columnName1 ON tableName
FOR EACH ROW
BEGIN
if :new.columnName1 = 3 AND :old.columnName1 = 1 then
:new.columnName2 := MOD(sequenceName.NextVal, 5) + 1 ;
end if;
END;
Related
Good day everyone,
I'm having trouble making my trigger work. As far as the functionality of the body and how it behaves, it does exactly as I intended for it to behave. However, when I start to fire the trigger, it returns an error in which Triggers should not have a SELECT statement from inside the main body. I'm still fairly new to coding and how to materialize the idea in my head into code. Hopefully someone could point me in a right direction on how change the Trigger I would like to have as a final result. Please see below script.
Update: Expected result would be whenever a user UPDATE a row and INSERT a new record via the application or job being run in the background, S1_HOVER_REPORT column would be updated with the value from the SELECT script and use the data from the S1_HOVER case result.
(Edit: I have updated the details of the problem above, added the Table being used and Error return)
Table: SITE
Column Name Type
------------------------------
ID VARCHAR2(14)
NAME VARCHAR2(70)
TYPE_CODE VARCHAR2(2)
PARENT VARCHAR2(14)
S1_HOVER_REPORT VARCHAR2(14)
CREATE OR REPLACE TRIGGER MESS.S1_HOVER_REPORT
AFTER INSERT OR UPDATE ON MESS.SITE
FOR EACH ROW
BEGIN
UPDATE (SELECT S1.ID,
S1.NAME,
S1.TYPE_CODE,
S1.PARENT AS PARENT1,
S2.PARENT AS PARENT2,
S1.S1_HOVER_REPORT,
CASE
WHEN (S1.TYPE_CODE = 'H2') THEN S1.PARENT
WHEN (S1.TYPE_CODE = 'S1') THEN S2.PARENT
ELSE S1.ID
END AS S1_HOVER
FROM SITE S1,
(SELECT ID,
NAME,
PARENT,
TYPE_CODE
FROM site
WHERE type_code='H2') S2
WHERE S1.PARENT=S2.ID
OR S1.ID = S2.PARENT) S3
SET S3.S1_HOVER_REPORT = S3.S1_HOVER;
END;
Error returned when Trigger fired:
Error report -
SQL Error: ORA-01779: cannot modify a column which maps to a non key-preserved table
ORA-06512: at "MES.S1_HOVER_REPORT", line 2
ORA-04088: error during execution of trigger 'MES.S1_HOVER_REPORT'
01779. 00000 - "cannot modify a column which maps to a non key-preserved table"
*Cause: An attempt was made to insert or update columns of a join view which
map to a non-key-preserved table.
*Action: Modify the underlying base tables directly.
(Update: I have included the updated trigger and it's now compiling without any issue, but I'm having errors whenever I try updating a record)
CREATE OR REPLACE TRIGGER MESS.S1_HOVER_REPORT
BEFORE INSERT OR UPDATE ON MESS.SITE
FOR EACH ROW
DECLARE
v_S1_HOVER_REPORT VARCHAR2(14);
BEGIN
SELECT CASE
WHEN (S1.TYPE_CODE = 'H2') THEN S1.PARENT
WHEN (S1.TYPE_CODE = 'S1') THEN S2.PARENT
ELSE (S1.ID)
END AS S1_HOVER
INTO v_S1_HOVER_REPORT
FROM SITE S1,
(SELECT ID,
NAME,
PARENT,
TYPE_CODE
FROM site
WHERE type_code='H2') S2
WHERE S1.PARENT=S2.ID
OR S1.ID = S2.PARENT;
:NEW.S1_HOVER_REPORT := v_S1_HOVER_REPORT;
END;
Error report -
SQL Error: ORA-04091: table MES.SITE is mutating, trigger/function may not see it
ORA-06512: at "MES.S1_HOVER_REPORT", line 4
ORA-04088: error during execution of trigger 'MES.S1_HOVER_REPORT'
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.
Firstly from the error message
An attempt was made to insert or update columns of a join view which map to a non-key-preserved table.
S3 is the veiw ( you are creating the view by doing a select inside an update statment). You can try and change this to have key preservation but I really wouldn't know how.
The error suggests updating the base tables not the view. So as mentioned in the comments :old and :new are your friend.
:OLD holds all the values of the table the trigger is created on BEFORE the update (null if insert)
:NEW holds all the values of the table the trigger is created on AFTER the update / insert.....
So if I understand what you want to do correctly you would need to...
declare a variable eg v_S1_hover_report
do your select returning whatever value you need into this variable
set the value in your site table by doing
:NEW.S1_HOVER_REPORT := v_S1_hover_report
By setting this value into the :NEW object when a commit happens it will be committed to the database. This completely removes the need for an update statement in the trigger.
You can also use :NEW.id in your select statement to filter it down to the record you are updating if it is helpfull
CREATE OR REPLACE TRIGGER MESS.S1_HOVER_REPORT
AFTER INSERT OR UPDATE ON MESS.SITE
FOR EACH ROW
v_test varchar2(10);
BEGIN
select 'Your value' into v_test from dual;
:new.s1_hover_report := v_test;
END;
OR
CREATE OR REPLACE TRIGGER MESS.S1_HOVER_REPORT
AFTER INSERT OR UPDATE ON MESS.SITE
FOR EACH ROW
v_test varchar2(10);
BEGIN
select 'Your value' into :new.s1_hover_report from dual;
END;
I have been trying to get the following trigger to update the price based on a discount rate when that rate is updated.
create or replace trigger discount_change
before update on maintain
for each row
begin
if :NEW.discount_rate != :OLD.discount_rate
then
update maintain m
set m.discount_price = :NEW.discount_rate * :OLD.price
where m.m_id = :NEW.m_id;
end if;
end;
As this is an exercise, these are in the same table (sadly) and when I attempt to do this I am consistently getting the error:
table %s.%s is mutating, trigger/function may not see it
I've been combing these forums looking for the answer and as far as I have researched, this shouldn't be throwing this error as I am not selecting anything from the table. What am I doing wrong here?
I would suggest to make the column discount_price as computed column so there is no need to define trigger for its updation
CREATE TRIGGER Alerta_Trig
BEFORE INSERT OR UPDATE OF Valor ON Medicao
FOR EACH ROW
WHEN (NEW.VALOR > 40)
BEGIN
UPDATE SENSOR
SET ALERTA = 'Alerta laranja'
WHERE SENSOR_ID=(SELECT SENSOR_SENSOR_ID FROM MEDICAO);
END;
I have this table of Sensors that are supposed to receive a String with an alert if 1 value > 40 enters the Medicao table. Any thoughs?
As you haven't said quite what is wrong I'm speculating a bit, but I can see two immediate potential problems.
You shouldn't query the same table again, generally, as you'll risk get a mutating table error. You also have no condition in the subquery so you'd be likely to get multiple rows back, and a subquery-returns-more-than-one-row error.
Presumably you're doing that subquery to try to get the ID value from the affected row, so use the :NEW pseudo-row for that:
CREATE TRIGGER Alerta_Trig
BEFORE INSERT OR UPDATE OF Valor ON Medicao
FOR EACH ROW
WHEN (NEW.VALOR > 40)
BEGIN
UPDATE SENSOR
SET ALERTA = 'Alerta laranja'
WHERE SENSOR_ID = :NEW.SENSOR_SENSOR_ID;
END;
I have table A and there is a column name COL_A.
I want that if someone change the value, lets say from 1 to 'X' (not costant) that the trigger will change it back from 'X' to 1.
SQLite does not support changing the new column values.
The only way to change a column in a trigger would be to run an UPDATE command,
but that would run the trigger again.
What you can do is to prevent changing the column in the first place:
CREATE TRIGGER IF NOT EXISTS prevent_col_a_change
BEFORE UPDATE OF col_a ON a
BEGIN
SELECT RAISE(ABORT, 'COL_A must not be changed');
END;
UPDATE trigger is a good solution for your case. Just set old value to the new value, that will lead to behaviour you want.
For example:
CREATE OR REPLACE TRIGGER orders_before_update
BEFORE UPDATE
ON orders
FOR EACH ROW
BEGIN
:new.CreatedAt:= :old.CreatedAt;
END;
I'm using DB2 and want to update several rows that meet my condition with the same next value from my sequence.
Here is what I tried but this doesn't work as the next value is fetched for each row:-
update dependency dep set vid=NEXT VALUE FOR seq_VID where id in ('8371','8372','8373')
id is the the primary key and seq_VID is a sequence. So what I had hoped was that say the next sequence value was 99, that 99 would be set for all 3 rows (and not 99,100,101 as is the case with this).
My workaround is to break it into separate statements for each id in my list, i.e.
update dependency dep set vid=NEXT VALUE FOR seq_VID where id= ('8371')
update dependency dep set vid=PREVIOUS VALUE FOR seq_VID where id= ('8372')
update dependency dep set vid=PREVIOUS VALUE FOR seq_VID where id= ('8373')
But I'd like to execute this in one SQL statement if possible - any ideas?
If you always knew that you wanted to put the 'previous' sequence value on the two rows after the one you updated with the 'next' next value you might be able to use a compound trigger, similar to the following (Oracle syntax, please forgive):
CREATE OR REPLACE TRIGGER DEPENDENCY_COMPOUND
FOR UPDATE ON DEPENDENCY
COMPOUND TRIGGER
TYPE tDependency_row_table IS TABLE OF DEPENDENCY%ROWTYPE;
tblDependency_rows tDependency_row_table := tDependency_row_table();
AFTER EACH ROW IS
BEGIN
tblDependency_rows.EXTEND;
tblDependency_rows(tblDependency_rows.LAST).ID = NEW.ID+1;
tblDependency_rows(tblDependency_rows.LAST).VID = seq_VID.CURRVAL;
tblDependency_rows.EXTEND;
tblDependency_rows(tblDependency_rows.LAST).ID = NEW.ID+2;
tblDependency_rows(tblDependency_rows.LAST).VID = seq_VID.CURRVAL;
END;
AFTER STATEMENT IS
BEGIN
FOR i IN tblDependency_rows.FIRST..tblDependency_rows.LAST LOOP
UPDATE DEPENDENCY
SET VID = tblDependency_rows(i).VID
WHERE ID = tblDependency_rows(i).ID;
END LOOP;
END;
END DEPENDENCY_AU;
Then you'd issue your update statement as
UPDATE DEPENDENCY
SET VID = seq_VID.NEXTVAL
WHERE ID = 8371;
and the trigger should take care of updating the other two rows.
The compound trigger is useful in Oracle 11+ to help work around the 'mutating table' error, which occurs when a trigger attempts to SELECT, INSERT, UPDATE, or DELETE data in the same table which the trigger is on.
This is a rather contrived situation and makes some huge assumptions about which rows should be updated, but perhaps it will prove useful.
Share and enjoy.