Use Procedure instead of trigger in MySQL

iszxjhcz  于 2022-12-22  发布在  Mysql
关注(0)|答案(1)|浏览(115)

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?

kyxcudwk

kyxcudwk1#

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

相关问题