I need to update the parent item's updated_at field when any of it's child item's updated_at field is updated.
DELIMITER $$
CREATE TRIGGER after_item_update
AFTER UPDATE
ON item FOR EACH ROW
BEGIN
IF OLD.updated_at <> NEW.updated_at AND OLD.parent_id > 0 THEN
UPDATE item i set updated_at = now() where i.id = OLD.parent_id;
END IF;
END$$
DELIMITER ;
I tried above query but it gives me following error when I try to update any row from the table.
ERROR 1442 (HY000): Can't update table 'item' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.```
Related
Error Code: 1442. Can't update table 'participant' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
So I want to achieve this -> once the participant is updated I want to update his create_date to current date
drop trigger update_user;
DELIMITER //
CREATE
TRIGGER update_user BEFORE UPDATE
ON Competition.participant
FOR EACH ROW BEGIN
update participant
set create_date = CURRENT_TIMESTAMP
where id_participant = old.id_participant;
END//
DELIMITER ;
update participant
set name = 'Rostyk'
where id_participant = 1;
CREATE
TRIGGER update_user
BEFORE UPDATE
ON Competition.participant
FOR EACH ROW
SET NEW.create_date = CURRENT_TIMESTAMP;
But it is more simple to define create_date column as ON UPDATE CURRENT_TIMESTAMP, and do not use triggers.
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.
As the title says, I have a table with a column named last_update of type DATE. I want to make a trigger that set it to CURDATE() every time I update a row(set it for this particular row). I tried this:
delimiter $$
CREATE TRIGGER before_customer_update before update on customer for each row
begin
update customer set last_update = CURDATE() where id = old.id;
end $$
delimiter ;
but a get an error
can't update table in stored function/trigger because it is already
used by statement
How can I resolve this?
It would be easier to change the column definition to
last_update timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
If you really want to do it in a trigger (refer to the current record with NEW):
Replace
update customer set last_update = CURDATE() where id = old.id;
with
set NEW.last_update = CURDATE();
DELIMITER //
CREATE TRIGGER `Salary` AFTER UPDATE ON `employee`
FOR EACH ROW BEGIN
IF OLD.time_out <> NEW.time_out THEN
UPDATE employee SET time_work=TIMEDIFF(time_out,time_in);
END IF;
END;
DELIMITER ;
AND I USE THIS QUERY
UPDATE DataBase.employee SET time_out='2014-10-22 16:41:09' WHERE IDREC='4945'
THERE Error Code : 1442
Can't update table 'employee' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
You cannot update a table (employee) 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 'employee' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
Solution: instead of AFTER UPDATE ON, make it a BEFORE UPDATE ON trigger, to set the new value for the field, before inserting - if time_work is a field in the new entry, you need to update just before insert - the probable SQL 'd be (not tested):
DELIMITER //
CREATE TRIGGER `Salary` BEFORE UPDATE ON `employee`
FOR EACH ROW BEGIN
IF OLD.time_out <> NEW.time_out THEN
SET NEW.time_work=TIMEDIFF(time_out,time_in);
END IF;
END;
DELIMITER ;
delimiter //
create trigger T1 before update on account
for each row
if NEW.balance <= 0 then
update account set balance=OLD.balance;
end if;
//
Query OK, 0 rows affected (0.09 sec)
delimiter ;
update account set balance=-1 where id=101;
ERROR 1442 (HY000): Can't update table 'account' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
MySQL uses row level triggers, so the trigger is fired for each row that is being changed. Because of that there is no reason to update the target table, just assign the value:
create trigger T1 before update on account
for each row
if NEW.balance <= 0 then
set new.balance = OLD.balance; -- this is the difference
end if;
SQLFiddle example: http://sqlfiddle.com/#!2/34aebb/1
Your update wouldn't have worked anyway because it was missing a where clause which means you would have updated the entire table just because a single row was changed.