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.
Related
I'm trying to remove a row after inserted in a temp table and I get this error:
INSERT INTO `temp_program_counter` (`id`, `program_id`) values (NULL, '275')
Can't update table 'temp_program_counter' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
And here is my trigger:
DELIMITER $$
DROP TRIGGER IF EXISTS `testtemp`$$
CREATE
TRIGGER `testtemp` AFTER INSERT ON `temp_program_counter`
FOR EACH ROW BEGIN
UPDATE `program` SET `view_count`=`view_count`+1;
DELETE FROM `temp_program_counter` WHERE id=new.id;
END;
$$
DELIMITER ;
this table is a temp table that get's data with delay and after receiving must update the main table and be deleted from temp.
thanks for helping in advance.
You probably have already resolved this in some way but I'll post the answer anyway:
You cannot refer to the same table that triggered the trigger in a trigger (or a combination of trigger and stored procedure). This could mean an infinite loop, so sql prevents it by giving that error, resulting in always the same error.
Try BEFORE INSERT
Because, you are trying to delete a record which has just been inserted but may be not yet committed in this session. Hence you are getting the error.
If you use BEFORE INSERT, then all other record in the table with id=new.id would be deleted and the new record will be inserted successfully.
DELIMITER $$
DROP TRIGGER IF EXISTS `testtemp`$$
CREATE
TRIGGER `testtemp` BEFORE INSERT ON `temp_program_counter`
FOR EACH ROW BEGIN
UPDATE `program` SET `view_count`=`view_count`+1;
DELETE FROM `temp_program_counter` WHERE id=new.id;
END;
$$
DELIMITER ;
I am trying to insert some records to a table and update its auto incriment id in another table;
I have a trigger
delimiter #
create trigger test AFTER INSERT ON samples_track for each row begin
update sample_details set sampletrack_id = new.sampletrack_id where sample_details.sample_id = new.sample_id; end#
delimiter ;
And I am running a query
INSERT INTO samples_track(sample_name,customer_id,contact_name,added_by,added_date,status,sample_id)
SELECT sample_name,
customer_id,
clinician_id,
999,
'2016-01-23',
1,
sample_id
FROM sample_details
WHERE sampletrack_id IS NULL;
The result is as below
Error in query (1442): Can't update table 'sample_details' in stored
function/trigger because it is already used by statement which invoked
this stored function/trigger.
Well, the reason of this error is that when you insert a record mysql locks the table. you can't insert/update/delete rows of the same table where you insert.. because then the trigger would called again and again.. ending up in a recursion.
To solve your problem, have a look at this thread. Also, in this forum they suggest to use a before insert trigger... Worth a try.
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.
I'm trying to remove a row after inserted in a temp table and I get this error:
INSERT INTO `temp_program_counter` (`id`, `program_id`) values (NULL, '275')
Can't update table 'temp_program_counter' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
And here is my trigger:
DELIMITER $$
DROP TRIGGER IF EXISTS `testtemp`$$
CREATE
TRIGGER `testtemp` AFTER INSERT ON `temp_program_counter`
FOR EACH ROW BEGIN
UPDATE `program` SET `view_count`=`view_count`+1;
DELETE FROM `temp_program_counter` WHERE id=new.id;
END;
$$
DELIMITER ;
this table is a temp table that get's data with delay and after receiving must update the main table and be deleted from temp.
thanks for helping in advance.
You probably have already resolved this in some way but I'll post the answer anyway:
You cannot refer to the same table that triggered the trigger in a trigger (or a combination of trigger and stored procedure). This could mean an infinite loop, so sql prevents it by giving that error, resulting in always the same error.
Try BEFORE INSERT
Because, you are trying to delete a record which has just been inserted but may be not yet committed in this session. Hence you are getting the error.
If you use BEFORE INSERT, then all other record in the table with id=new.id would be deleted and the new record will be inserted successfully.
DELIMITER $$
DROP TRIGGER IF EXISTS `testtemp`$$
CREATE
TRIGGER `testtemp` BEFORE INSERT ON `temp_program_counter`
FOR EACH ROW BEGIN
UPDATE `program` SET `view_count`=`view_count`+1;
DELETE FROM `temp_program_counter` WHERE id=new.id;
END;
$$
DELIMITER ;
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.