I created the following trigger in Mysql:
DELIMITER //
CREATE TRIGGER delete_G8_dados_indicadores_antes_insert
BEFORE INSERT
ON G8_dados_indicadores FOR EACH ROW
BEGIN
delete from G8_dados_indicadores where id_indicador=NEW.id_indicador and ano = NEW.ano;
END; //
DELIMITER ;
The idea is that before entering a record in G8_dados_indicadores table, the system triggers the trigger automatically removing existing records. But to run an insert, mysql returns the following error:
1442 - Can't update table 'G8_dados_indicadores' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
What can it be?
This is a known limitation in mysql, you have to find a workaround. In this particular instance, I would not even use a trigger, but either use replace into ... or insert ... on duplicate key update ... instead of an insert.
Replace into deletes the old row and inserts the new one, insert ... on duplicate key update ... does an update instead of an insert, if the record already exists.
Related
I have the following trigger:
CREATE DEFINER=root#localhost TRIGGER after_insert_student AFTER INSERT ON
students FOR EACH ROW BEGIN
SET #cateID := NEW.ID ;
IF #cateID IS NOT NULL THEN
SELECT right(max(id), 3) INTO #firstid FROM students LIMIT 1;
SET #IDFOR = #firstid+1;
SET #DATEID = DATE_FORMAT(NOW(),'%y%d%m');
INSERT INTO students (CONCAT(#DATEID, #IDFOR), DATENEW)
VALUES(#IDFOR, NOW() );
END IF;
END
ERROR:
ERROR 1442: 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 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 'MyTable' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
If all you need is to set the fields value based on previous entries from the same table or other (using SELECT). You can use BEFORE INSERT ON students trigger, this way you can set/update/edit the field values using the NEW operator.
Is it because you are actually doing the insert on a before insert trigger?
Wouldn't you leave the insert up to the event that fired it?
You'd be inserting another student based on the student you have just inserted in the same table
Try rewriting with an AFTER INSERT ON, and update the record you have just created instead.
The error message says all: your trigger handles before insert events on the students table and you attempt to insert a new record into the students table from the trigger. Which would set off this very trigger again, which would insert another row into the same table... Shall I go on?
Change it to an after insert trigger or using the NEW.fieldname=... syntax you can modify the inserted values.
I have created a table test1 as
CREATE TABLE test1
(id INT,
name VARCHAR(20))
ENGINE=INNODB;
Now I want to delete a record from this table before inserting a new record having the same id(that is,record already existing with same id,then insert the new record) .I tried the code give below.
create trigger before_insert_test1 before insert
on test1
for each row
begin
delete from test1 where id=NEW.id;
end;
but it shows error
ERROR 1442 (HY000): Can't update table 'test1' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
Can anyone help.
thanks in advance
I don't think you can update/delete from the same table that you're inserting into. The table is being locked by the insert statement, even though you specified after insert. The insert transaction is not yet completed.
My suggestion would be to do a test and delete the record before you do the insert, or use the MySQL ON DUPLICATE KEY UPDATE statement if it applies
create trigger before_insert_test1 before insert on test1
DELETE FROM test1 WHERE id=NEW.id;
end;
You shan't need for loop, delete statement will delete every record with given id.
CREATE TRIGGER abc
after insert ON visadetails
FOR EACH ROW update visadetails SET VisaId=concat(new.Occupation,'-',new.Vid);
insert into visadetails (Occupation,Destination) value("student","rome");
after creating trigger m not able to insert the value, it gives following error message
#1442 - Can't update table 'gallery' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
No need for an update (which btw would have updated all rows in the table with the result of the concat() for the new row because of the missing where clause).
CREATE TRIGGER abc
after insert ON visadetails
FOR EACH ROW
SET new.VisaId=concat(new.Occupation,'-',new.Vid);
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'm trying to create a trigger that will allow only one record in a database, so it would delete any previous records.
But currently, it doesn't allow me to insert anything it, because it's instantly deleted.
DELIMITER $$
CREATE TRIGGER test_insert
BEFORE INSERT ON test
FOR EACH ROW BEGIN
DELETE FROM test WHERE id = NEW.id - 1;
END$$
How would I delete a previously (or all previous) inserted record?
"But currently, it doesn't allow me to insert anything it, because it's instantly deleted."
Acutally, when you do an INSERT, the execution of your trigger should be throwing exception:
Error Code: 1442
Can't update table 'test' in stored function/trigger because it is
already used by statement which invoked this stored function/trigger.
(Unless something is radically different in a newer version of MySQL.)
The operation you want to perform (i.e. deleting rows from the same table you are inserting into) cannot be done in a MySQL trigger.
You could use a combination of a UNIQUE KEY and a BEFORE INSERT trigger to prevent more than one row from being inserted. The BEFORE INSERT trigger could set the value of the column that has a unique
key on it to be a static value, then the INSERT statement would throw a duplicate key ("Duplicate entry") exception.
Then, you could use an INSERT ... ON DUPLICATE KEY UPDATE ... statement to update the values of columns other than the unique id, e.g.
CREATE TRIGGER `test_insert` BEFORE INSERT ON test
FOR EACH ROW BEGIN
SET NEW.id := 1;
END
ALTER TABLE test ADD UNIQUE KEY (id);
INSERT INTO test (somecol) VALUES ('someval')
ON DUPLICATE KEY UPDATE somecol = VALUES(somecol) ;
To do that you first need to find some value at which you want to delete all and insert for. This way you don't need a trigger, you simply delete all previous cases and then add a new row. Unless for some reason unexplained from your code you would need a trigger a simple solution in your loop could work Like:
$query = mysql_query("DELETE FROM test WHERE id = NEW.id -1");
$new_id = $new.id -1;
$query2 = mysql_query("INSERT INTO test VALUES('$new_id','$var1','$var2'));