gpt4 book ai didi

MySql错误: Can't update table in stored function/trigger because it is already used by statement which invoked this stored function/trigger

转载 作者:行者123 更新时间:2023-11-29 15:25:26 24 4
gpt4 key购买 nike

我正在运行 MySQL 查询。但是,当从表单输入添加新行时,我收到此错误:

Error: Can't update table 'brandnames' in stored function/trigger because it is 
already used by statement which invoked this stored function/trigger.

从代码中:

CREATE TRIGGER `capital` AFTER INSERT ON `brandnames`
FOR EACH
ROW UPDATE brandnames
SET bname = CONCAT( UCASE( LEFT( bname, 1 ) ) , LCASE( SUBSTRING( bname, 2 ) ) )

这个错误是什么意思?

最佳答案

INSERT 触发器触发时,您无法更改表。 INSERT 可能会执行一些锁定操作,这可能会导致死锁。此外,从触发器更新表将导致同一触发器在无限递归循环中再次触发。这两个原因都是 MySQL 阻止您这样做的原因。

但是,根据您想要实现的目标,您可以使用 NEW.fieldname 访问新值,甚至可以使用旧值 - 如果执行 UPDATE --OLD

如果您有一个名为 full_brand_name 的行,并且您想在 small_name 字段中使用前两个字母作为短名称,您可以使用:

CREATE TRIGGER `capital` BEFORE INSERT ON `brandnames`
FOR EACH ROW BEGIN
SET NEW.short_name = CONCAT(UCASE(LEFT(NEW.full_name,1)) , LCASE(SUBSTRING(NEW.full_name,2)))
END

关于MySql错误: Can't update table in stored function/trigger because it is already used by statement which invoked this stored function/trigger,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59118363/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com