Error when Updating a Table with Trigger - mysql

I have a trigger setup that every time a new table is created, it reads the ID (which is in varchar) and converts it to integer and stores it in another column.
However when I run it I encounter the following error:
Can't update table 'products' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
This is my trigger:
CREATE TRIGGER `var_to_int` AFTER INSERT ON `products`
FOR EACH ROW UPDATE `products`
SET `int_id`= CAST(`var_id` AS INT)
WHERE `int_id` IS NULL OR `int_id` = ''
How do I prevent this?
This error is thrown by the PHP script creating a new item in the table.

You can not update the table itself.
Trigger starts when it is UPDATE, and then it has to do UPDATE. An endless loop is formed. Try using an additional table to store temporary data to later update the correct table

Related

Use Procedure instead of trigger in MySQL

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

Before/after delete trigger

I have the following trigger in my DB table:
CREATE TRIGGER `ordernumberdelete` AFTER DELETE ON `classes`
FOR EACH ROW UPDATE classes set ordernumber = ordernumber-1 where ordernumber>old.ordernumber
which basically drops a value in an ordernumber column by one if it is higher than the deleted row's order number - plain and simple.
The call to delete the row is using PHP/ajax/mysqli prepared statements but I am getting the error:
can't update table 'classes' in stored function/trigger because it is already used by statement which invokes this stored function/trigger in ....
Looked all over SO/Google and can't see a solution. I though the trigger was done after the mysqli_stmt_execute call.
Any ideas?

how to run trigger after update table then running statement update in same table

I have table machine and I would like to create trigger, the condition is when table machine updates the trigger will be running script update table machine again where field status_1 = '' OR status_2 = ''
CREATE TRIGGER `update_machine` AFTER UPDATE ON `machine`
FOR EACH ROW
UPDATE `machine`
SET `status_1`='disabled',`status_1`='disabled'
WHERE `status_1`='' OR `status_2`=''
but the error say
1442 - Can't update table 'machine' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
but how can I still run this trigger?
A Trigger can't update another row of the same table.
A typical way to do this is to create a stored procedure that updates your table and then update other rows of the same table.

mysql error 1442 trigger

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.

modify the table from its own trigger in Mysql- PhpMyAdmin

I'm using Mysql in phpMyAdmin,where i have to delete a entry from tableA if i insert a row with same primary key.I thought to do it in trigger of tableA BEFORE INSERT
For Ex,
if the tableA contains
1 Hai Hello
here 1 is the primary key
And now if i insert a row 1 Bye Hello then the trigger BEFORE INSERT will delete the old entry and then the new row (2nd) will be inserted. But Mysql has restriction of not being able to update a table inside a trigger defined for that same table.
It gives the error
#1442 - Can't update table 'tableA' in stored
function/trigger because it is already used by statement which invoked
this stored function/trigger.
So i changed my way, i called a procedure from trigger BEFORE INSERT of tableA and in that procedure i do the task what i thought to do in trigger. But unfortunately i'm getting the same error.
In trigger BEFORE INSERT i simply called the procedure as
CALL proce1(new.Reg_No);
In procedure i have done this
DECLARE toup integer;
select count(*) into toup from tableA where Reg_No=reg;/*Here Reg_No is primary key */
if toup > 0 then
delete from tableA where Reg_No=reg;
end if;
Need some other Idea to achieve this. Help Me.....
I don't like to use triggers so much because they are hard to manage somethimes. Also they will cause you a downgrade in performance. I am not against trigger as they can be handy in some cases.
In your case have you though of using REPLACE(....) or INSERT INTO .... ON DUPLICATE KEY UPDATE or even INSERT INTO IGNORE .....
REPLACE(....) will delete a record if a record is found and insert a new one with the same ID.
INSERT INTO .... ON DUPLICATE KEY UPDATE will allow you to override existing field if a duplicate is found.
INSERT INTO IGNORE ..... will allow you to ignore the new inserted row if one already exists
Since you mentioned in the comments that you are importing records from a file, then try to use LOAD DATA INFILE logic which will allow you to REPLACE field on duplicate
LOAD DATA LOCAL INFILE 'x3export.txt'
REPLACE INTO TABLE x3export