Use Procedure instead of trigger in MySQL - 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

Related

MYSQL Update Trigger - Can't update table in stored function/trigger because it is already used by statement

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.

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.

Error Code: 1442. Can't update table 'students' in stored function/trigger

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.

How to create a trigger for the table already being used

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);

Mysql triggers Create error

This is my create triggers . I want when Insert vhftofass it execute this query(UPDATE vhf_msg_rx SET msg_text="";) that means Its make Null this table vhf_msg_rx.But when i write that query after insert Query it make error (Can't update table 'vhf_msg_rx' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.) Please help me how can i do this
CREATE
/*!50017 DEFINER = 'root'#'localhost' */
TRIGGER `VHFtoFASS` AFTER UPDATE ON `vhf_msg_rx`
FOR EACH ROW BEGIN
INSERT INTO vhftofass SELECT NULL,msg_text,NOW(),0 FROM vhf_msg_rx WHERE msg_text<>"";
END;
$$
I'm not completely clear on what you're trying to do, but it seems like you want to copy modified rows of one table into another, but only retaining certain fields. You could do that by referencing the inserted record with NEW.columnname inside the FOR EACH ROW loop, for example
CREATE TRIGGER `VHFtoFASS` AFTER UPDATE ON `vhf_msg_rx`
FOR EACH ROW BEGIN
INSERT INTO vhftofass VALUES(NULL,NEW.msg_text,NOW(), 0);
END;
Note that this will only handle updates - you'll need an INSERT trigger to capture new rows.