Trigger before update causes error #1442 - mysql

Hello I have a problem with this trigger:
CREATE TRIGGER sprLog AFTER UPDATE ON Users FOR WACH ROW
BEGIN
IF OLD.wl < NEW.wl THEN
IF NEW.wl = 3 THEN
INSERT INTO blocked(uid, time) VALUE(NEW.ID, 15);
UPDATE Users SET state=3 WHERE ID=NEW.ID;
END IF;
END IF;
END;
Now when i try to execute this query: UPDATE Users SET wl=3 WHERE ID=1
I have error:
#1442 - Can't update table 'Users' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
I hope that you understand from trigger what I want to do. I know that this trigger causes itself, but I don't have idea how to do that in other way. I tried using two trigger one After Update on Users and second After Insert On blocked, but the problem were the same.

Create a before update trigger if you want to modify the row being updated.
CREATE TRIGGER sprLog BEFORE UPDATE ON Users FOR WACH ROW
BEGIN
IF OLD.wl < NEW.wl THEN
IF NEW.wl = 3 THEN
INSERT INTO blocked(uid, time) VALUE(NEW.ID, 15);
SET NEW.state = 3;
END IF;
END IF;
END;

Related

Update Trigger on same table MySQL 5.6

I have a table 'foods' with columns 'featured', 'restaurant_id', 'retaurant_stock'.
How can I update column 'restaurant_stock' using trigger or any other solution available on mySQL version 5.6 after update of column 'featured' on the same table on a condition?
Calculated columns are not available on mySQL version 5.6.
Here's my trigger:
CREATE TRIGGER update_restaurant_stock BEFORE UPDATE ON foods
OR EACH ROW
BEGIN
IF NEW.featured = 1 THEN
UPDATE `foods` SET NEW.restaurant_stock = (NEW.restaurant_id);
ELSE
UPDATE `foods` SET NEW.restaurant_stock = 0;
END IF;
END
Now when I update column 'featured' I get the following error message:
#1442 - Can't update table 'foods' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
Thanks for you help!
You can't update the current row that way, you can use set to change the new values
DELIMITER $$
CREATE TRIGGER update_restaurant_stock BEFORE UPDATE ON foods
OR EACH ROW
BEGIN
IF NEW.featured = 1 THEN
SET NEW.restaurant_stock = (NEW.restaurant_id);
ELSE
SET NEW.restaurant_stock = 0;
END IF;
END$$
DELIMITER ;
CREATE TRIGGER tr_bu_foods
BEFORE UPDATE
ON foods
FOR EACH ROW
-- update restaurant_stock
SET NEW.restaurant_stock = NEW.restaurant_id * (NEW.featured = 1);

CREATE TRIGGER to monitor a specific table row

I have two tables & need to write a TRIGGER for this scenario.
1. Parameters_Table(int pkid, string param_name, string param_value)
2. Tokens_Table(int pkid,string item,string validity)
I have a requirement of deleting all entries in Tokens_Table if a particular row where param_name='Enterprise' in Parameters_Table changes its 'param_value'. The value is changed from UI. eg., row <7,enterprise,60> changes to <7,enterprise,25> in Parameters_Table then delete all entries from Tokens_Table.
How should I write the trigger. Please guide. (I need to write this for Informix Database, but mysql queries would also help)
DELIMITER $$
CREATE TRIGGER after_update AFTER UPDATE ON Parameters_Table
FOR EACH ROW
BEGIN
IF OLD.param_name ='Enterprise' THEN
IF NEW.param_value != OLD.param_value THEN
DELETE FROM Tokens_Table WHERE pkid = NEW.pkid;
END IF;
END IF;
END$$
Just try above code.Hope this will helps.
You can try the following syntax/format:
DELIMITER $$
CREATE TRIGGER delete_tokens AFTER INSERT ON Parameters_Table
FOR EACH ROW
BEGIN
IF NEW.param_name = 'enterprise' THEN
BEGIN
DELETE FROM Tokens_Table WHERE pkid = NEW.pkid;
END;
END IF;
END$$

Error trigger insert into after insert

Hi guys I am trying to make some triggers to update in one table after insert in other table but it reports me an error. Two tables have the same fields and with the same name.
Here is the code.
DELIMITER //
CREATE TRIGGER Insertar_Bares_Private AFTER INSERT ON Bares
FOR EACH ROW
BEGIN
INSERT INTO Bares_Private (nombrebar, direccion) VALUES (new.nombrebar, new.direccion);
END//
CREATE TRIGGER Insertar_Categorias_Private AFTER INSERT ON Categorias
FOR EACH ROW
BEGIN
INSERT INTO Categorias_Private (nombrecategoria) VALUES (new.nombrecategoria);
END//
CREATE TRIGGER Insertar_Productos_Private AFTER INSERT ON Productos
FOR EACH ROW
BEGIN
INSERT INTO Productos_Private (nombreproducto, idcategoria, descripcion, precio, imagen) VALUES (new.nombreproducto, new.idcategoria, new.descripcion, new.precio, new.imagen);
END//
-- Triggers al actualizar
CREATE TRIGGER Actualizar_Usuarios_Private AFTER UPDATE on Usuarios
for each ROW
BEGIN
UPDATE Usuarios_Private
SET nombreusuario=new.nombreusuario, contrasenia=new.contrasenia, email=new.email, telefono=new.telefono
where idusuario=new.idusuario;
END//
CREATE TRIGGER Actualizar_Bares_Private AFTER UPDATE on Bares
for each ROW
BEGIN
UPDATE Bares_Private
SET nombrebar=new.nombrebar, direccion=new.direccion
where idbar=new.idbar;
END//
CREATE TRIGGER Actualizar_Categorias_Private AFTER UPDATE on Categorias
for each ROW
BEGIN
UPDATE Categorias_Private
SET nombrecategoria=new.nombrecategoria
where idcategoria=new.idcategoria;
END//
CREATE TRIGGER Actualizar_Productos_Private AFTER UPDATE on Productos
for each ROW
BEGIN
UPDATE Productos_Private
SET nombreproducto=new.nombreproducto, idcategoria=new.idcategoria, descripcion=new.descripcion, precio=new.precio, imagen=new.imagen
where idproducto=new.idproducto;
END//
DELIMITER ;
And here is the error reported.
#1235 - This version of MySQL doesn't yet support 'multiple triggers with the same action time and event for one table'
Thank you in advice.
Remove the ; from all the END; and apply the delimiter END// will solve your problem.
Since you already placed the proper ; for the INSERT/UPDATE statement, so no need to place it again in the END;.
You only need to change the delimiter before defining a trigger if the trigger contains more than one query. This is because you need to use the default delimiter ; in this case to separate the queries within the trigger, but the MySQL client will incorrectly think you mean to terminate the trigger definition here. This also requires you to use BEGIN ... END around the block of queries in the trigger.
DELIMITER //
CREATE TRIGGER Insertar_Usuarios_Private AFTER INSERT ON Usuarios
FOR EACH ROW
BEGIN
INSERT INTO Usuarios_Private (nombreusuario, contrasenia, email,telefono) VALUES (new.nombreusuario, new.contrasenia, new.email, new.telefono);
END//
CREATE TRIGGER Insertar_Bares_Private AFTER INSERT ON Bares
FOR EACH ROW
BEGIN
INSERT INTO Bares_Private (nombrebar, direccion) VALUES (new.nombrebar, new.direccion);
END//
-- and so on ...
DELIMITER ;
However, your triggers each only contain one query. Therefore you can dispense with the delimiter and the BEGIN ... END block entirely.
CREATE TRIGGER Insertar_Usuarios_Private AFTER INSERT ON Usuarios
FOR EACH ROW
INSERT INTO Usuarios_Private (nombreusuario, contrasenia, email,telefono) VALUES (new.nombreusuario, new.contrasenia, new.email, new.telefono);
CREATE TRIGGER Insertar_Bares_Private AFTER INSERT ON Bares
FOR EACH ROW
INSERT INTO Bares_Private (nombrebar, direccion) VALUES (new.nombrebar, new.direccion);
-- and so on ...

Two triggers and I am getting an error when updating table

I got the two following triggers, each one worked alone, but together they don't.
How can I make them to work together? They update different fields in table.
trigger 1:
create trigger wys_sk_u after update on `things`
for each row
begin
UPDATE `current` s
INNER JOIN things u ON s.id_thing = u.id_thing
INNER JOIN dude_base b ON b.id= s.id
SET s.`curr_cash` = u.cash1 * b.cash2/ 100;
end;
$$
trigger 2:
create trigger suma_u after update on `current`
for each row
begin
UPDATE `current`
SET `mysum` = `curr_cash` + `mysum`;
end;
$$
First one should update when cash1 or cash2 updates, and change value of curr_cash.
Second should update when curr_cash updates, and change mysum.
I got following error when I edit table things:
#1442 - Can't update table 'current' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
#edit
added new answear to the question.
What if I want to do something like this:
CREATE TRIGGER total BEFORE UPDATE ON `current` FOR EACH ROW
BEGIN
if new.`new_pay_date` <> old.`new_pay_date`
SET new.`total_cash` = new.`curr_cash` + new.`total_cash`;
end if;
END;
$$
Error:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SET new.`total_cash` = new.`curr_cash` + new.`total_cash`; end if;' at line 4
This was working without
if new.`new_pay_date` <> old.`new_pay_date`
end if;
But I need to check this, and only update with date change.
current table:
curr_cash
new_pay_date
id_person
id_thing
total_cash
Anyone can help me with this one?
The problem is in the second trigger. Try to use BEFORE UPDATE trigger to change field value using SET statement -
CREATE TRIGGER suma_u BEFORE UPDATE ON `current` FOR EACH ROW
BEGIN
SET new.`mysum` = new.`curr_cash` + new.`mysum`;
END;
It is not possible to update a table for for which the trigger is created in the trigger. There is locking mechanism on MySQL so that you can't update the row of the same table triggered the trigger, it can cause recursive call to the trigger and infinite loop.

How to write a trigger to abort delete in MYSQL?

I read this article but it seems not work for delete. I got this error when tried to create a trigger:
Executing SQL script in server
ERROR: Error 1363: There is no NEW row in on DELETE trigger
CREATE TRIGGER DeviceCatalog_PreventDeletion
BEFORE DELETE on DeviceCatalog
FOR EACH ROW
BEGIN
DECLARE dummy INT;
IF old.id = 1 or old.id =2 THEN
SELECT * FROM DeviceCatalog WHERE DeviceCatalog.id=NEW.id;
END IF;
END;
SQL script execution finished: statements: 4 succeeded, 1 failed
Improving #Devart's (accepted) answer with #MathewFoscarini's comment about MySQL SIGNAL Command, instead of raising an error by calling an inexistent procedure you could signal your custom error message.
DELIMITER $$
CREATE TRIGGER DeviceCatalog_PreventDeletion
BEFORE DELETE ON DeviceCatalog
FOR EACH ROW
BEGIN
IF old.id IN (1,2) THEN -- Will only abort deletion for specified IDs
SIGNAL SQLSTATE '45000' -- "unhandled user-defined exception"
-- Here comes your custom error message that will be returned by MySQL
SET MESSAGE_TEXT = 'This record is sacred! You are not allowed to remove it!!';
END IF;
END
$$
DELIMITER ;
The SQLSTATE 45000 was chosen as MySQL's Reference Manual suggests:
To signal a generic SQLSTATE value, use '45000', which means “unhandled user-defined exception.”
This way your custom message will be shown to the user whenever it tries to delete records ID 1 or 2. Also, if no records should be deleted from the table, you could just remove the IF .. THEN and END IF; lines. This would prevent ANY records from being deleted on the table.
Try something like this -
DELIMITER $$
CREATE TRIGGER trigger1
BEFORE DELETE
ON table1
FOR EACH ROW
BEGIN
IF OLD.id = 1 THEN -- Abort when trying to remove this record
CALL cannot_delete_error; -- raise an error to prevent deleting from the table
END IF;
END
$$
DELIMITER ;
Well, the error messages tells you quite clearly: in a DELETE trigger there is no NEW.
In an INSERT trigger you can access the new values with NEW..
In an UPDATE trigger you can access the new values with NEW., the old ones with - you guessed it - OLD.
In a DELETE trigger you can acces the old values with OLD..
It simply makes no sense to have NEW in a DELETE, just as OLD in an INSERT makes no sense.
As the error says: There is no NEW variable on delete.
you can use new.id only on insert and update. Use old.id instead.
SELECT * FROM DeviceCatalog WHERE DeviceCatalog.id=old.id;