I am having trouble with a trigger in MySQL. I have a column named "last_modified" that I want to be automatically updated with the current date and time when it is the table is edited. Using a trigger, this is my SQL query:
delimiter //
CREATE TRIGGER trg_update_responders BEFORE UPDATE ON survey_responders
FOR EACH ROW
BEGIN
UPDATE survey_responders
SET NEW.last_modified = CURRENT_DATETIME();
END;//
However, when I update the table, such as with this query:
UPDATE survey_responders SET first_name = "bob" WHERE id = "1";
MySQL Workbench displays error 1442: "Can't update table 'table_name' in stored function/trigger because it is already used by statement which invoked this stored function/trigger"
I have looked at similar questions with the same error but still have not fixed it. Help is appreciated.
** UPDATE **
This did the trick:
delimiter //
CREATE TRIGGER trg_update_responders BEFORE UPDATE ON survey_responders
FOR EACH ROW
BEGIN
SET NEW.last_modified = CURRENT_TIMESTAMP();
END;//
Seems like I simply did not need to repeat the
UPDATE survey_responders
and CURRENT_DATETIME() did not exist, I had to use CURRENT_TIMESTAMP().
This did the trick:
delimiter //
CREATE TRIGGER trg_update_responders BEFORE UPDATE ON survey_responders
FOR EACH ROW
BEGIN
SET NEW.last_modified = CURRENT_TIMESTAMP();
END;//
Seems like I simply did not need to repeat the
UPDATE survey_responders
and CURRENT_DATETIME() did not exist, I had to use CURRENT_TIMESTAMP().
What you are trying to do is not possible using a trigger.
Within a stored function or trigger, it is not permitted to modify a
table that is already being used (for reading or writing) by the
statement that invoked the function or trigger.
Source
You need to do this some other way.
See here: MySQL - Trigger for updating same table after insert
The typical way to do that, is to create a stored procedure, that
inserts into/Updates the target table, then updates the other row(s),
all in a transaction.
Related
I'm trying to make trigger to update one field in inserted row but even with this answer: https://stackoverflow.com/a/15300941/4018940 i cant make it work.
Every time i get error:
General error: 1442 Can't update table 'people' in stored
function/trigger because it is already used by statement which invoked
this stored function/trigger
Here is my code:
CREATE TRIGGER short_name_trigger
BEFORE INSERT ON `people`
FOR EACH ROW BEGIN
UPDATE `people` SET NEW.short_name = "wohahaha";
END
Is there any chance that stored procedure will make it work?
How to write that procedure?
If you want to change the value being inserted in the new row, you don't need to use UPDATE people. Just set NEW.short_name and this will replace the value that's being inserted.
CREATE TRIGGER short_name_trigger
BEFORE INSERT ON `people`
FOR EACH ROW BEGIN
SET NEW.short_name = "wohahaha";
END
I have a table named Client , I want to update the available credit column after updating the balance field.
I created this trigger
DELIMITER $$
CREATE TRIGGER client_update_balance
AFTER UPDATE ON client
FOR EACH ROW
BEGIN
update client
set AvailableCredit = CreditLimit - new.Balance;
END
But when I update a balance value like that
SET SQL_SAFE_UPDATES = 0;
UPDATE client
SET
balance = 1500
WHERE
client.ClientNum = 143;
I get this error:
Error Code: 1442. Can't update table 'client' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
Checked some StackOverflow answers and tried without Update statement inside
DELIMITER $$
CREATE TRIGGER client_update_balance
AFTER UPDATE ON client
FOR EACH ROW
BEGIN
set New.AvailableCredit = New.CreditLimit - New.Balance;
END;$$
DELIMITER ;
I get another error
Error Code: 1362. Updating of NEW row is not allowed in after trigger
Use a before update trigger and just set the value:
DELIMITER $$
CREATE TRIGGER client_update_balance
BEFORE UPDATE ON client
FOR EACH ROW
BEGIN
set new.AvailableCredit = new.CreditLimit - new.Balance;
END;
The idea is that you want to change values in the same row of the table being updated. You don't need a separate update transaction to do that. You just need to adjust the values.
That said, if AvailableCredit is always defined as this difference, you should use a generated column. That way, you don't have to update the value. It is just correct when you query the table.
I have the following trigger:
CREATE DEFINER=root#localhost TRIGGER after_insert_student after INSERT ON
students FOR EACH ROW BEGIN
SET #NEWID := NEW.ID ;
if #NEWID IS NOT NULL THEN
INSERT INTO students SET ID = #NEWID;
else
INSERT INTO students SET ID = 001;
END IF
END
ERROR:
Error Code: 1442. Can't update table 'students' in stored
function/trigger because it is already used by statement which
invoked this stored function/trigger.
You cannot change a table while the INSERT trigger is firing. The INSERT might do some locking which could result in a deadlock. Also, updating the table from a trigger would then cause the same trigger to fire again in an infinite recursive loop. Both of these reasons are why MySQL prevents you from doing this.
To do this, please reference this link
You probably want a BEFORE INSERT trigger. Then instead of updating the table, just assign the desired value to NEW.ID.
The problem is that you can't write statement like
INSERT INTO students SET ID = #NEWID;
because it requires to know which columns you want to insert, or if not columns specified then you insert into all columns values etc.
INSERT INTO students values (#NEWID);
should work or if you want to stick to your SET ID then try to write something like
UPDATE students SET ID = #NEWID;
Hope this guides you to solution to your problem.
Solution: make it a BEFORE UPDATE ON trigger, and use NEW operator, it'll do the job - as all you want is if new ID is NULL set it to 001:
CREATE DEFINER=root#localhost TRIGGER after_insert_student
BEFORE INSERT ON students
FOR EACH ROW BEGIN
IF NEW.ID IS NULL THEN
SET ID = 001;
END IF;
END;
Cause: You cannot update a table (students) where the trigger is invoked:
Within a stored function or trigger, it is not permitted to modify a
table that is already being used (for reading or writing) by the
statement that invoked the function or trigger.
Doing so will generate Error 1442:
Error Code: 1442
Can't update table 'chatroompost' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
My trigger fails when i try to insert a new row in my table because Mysql doesn't support updating rows in the same table the trigger is assigned to. Does anyone have suggestions on a good workaround/alternative?
My trigger:
-- Trigger DDL Statements
DELIMITER $$
CREATE TRIGGER check_sequence
BEFORE INSERT ON data FOR EACH ROW
BEGIN
IF EXISTS(SELECT TRUE FROM data WHERE sequence = NEW.sequence) THEN
UPDATE data SET sequence=sequence+1 WHERE sequence >= NEW.sequence;
END IF;
END $$
DELIMITER ;
Error Sql that is displayed when i try to insert new row:
ERROR 1442 (HY000): Can't update table 'data' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
Thanks for answer,
In MySQL, You can't update (or insert in) table A in a trigger for table A.
http://dev.mysql.com/doc/refman/5.0/en/stored-program-restrictions.html
You can try a sproc and use a transaction, or consider PostgreSQL, which can do this.
I currently have the following MySQL query, which I would like to include in a trigger, rather than as a separate query. The query updates data in two tables which are joinable:
UPDATE `testing_names` INNER JOIN `purchase_names`
ON `testing_names`.`fulldomain` = `purchase_names`.`fulldomain`
SET
`testing_names`.`account_id` = `purchase_names`.`account_id`,
`purchase_names`.`purchase_status` = 1
WHERE `purchase_names`.`purchase_status` = 0
A row is (separately) inserted into testing_names. This row SHOULD have a corresponding entry in purchase_names. After a row is inserted into testing_names, when my UPDATE query is next run, it will update testing_names.account_id and update purchase_names.purchase_status to effectively mark this task as completed.
It makes sense to run this as part of a trigger, but I haven't been able to create a trigger that does the job.
So far, I have successfully created a trigger:
DELIMITER $$
CREATE TRIGGER `my_trigger` AFTER INSERT
ON testing_names
FOR EACH ROW BEGIN
UPDATE `testing_names` INNER JOIN `purchase_names`
ON `testing_names`.`fulldomain` = `purchase_names`.`fulldomain`
SET
`testing_names`.`account_id` = `purchase_names`.`account_id`,
`purchase_names`.`purchase_status` = 1
WHERE `purchase_names`.`purchase_status` = 0;
END$$
DELIMITER ;
but evidently running a new UPDATE query is not allowed, because upon inserting a row, I get an error: #1442 - Can't update table 'testing_names' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
Unfortunately, a trigger on a table cannot update the same table.
From MySQL documentation:
Within a stored function or trigger, it is not permitted to modify a table that is already being used (for reading or writing) by the statement that invoked the function or trigger.
Instead, you might be able to complete your update by having the trigger on testing_names update purchase_names and have another trigger on purchase_names update testing_names.
Try Using a Before INsert Instead