61

我正在运行 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 ) ) )

这个错误是什么意思?

4

5 回答 5

87

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
于 2013-03-08T18:35:30.783 回答
8

正确的语法是:

FOR EACH ROW SET NEW.bname = CONCAT( UCASE( LEFT( NEW.bname, 1 ) )
                                   , LCASE( SUBSTRING( NEW.bname, 2 ) ) )
于 2013-03-08T18:35:04.020 回答
5

“BEFORE-INSERT”触发器是在插入时实现同表更新的唯一方法,并且只能从 MySQL 5.5+ 开始。但是,自动增量字段的值仅可用于“AFTER-INSERT”触发器 - 在 BEFORE 情况下默认为 0。因此,以下示例代码将根据自动增量值设置先前计算的代理键值id,但将编译,但实际上不会工作,因为 NEW.id 将始终为 0:

create table products(id int not null auto_increment, surrogatekey varchar(10), description text);
create trigger trgProductSurrogatekey before insert on product
for each row set NEW.surrogatekey = 
  (select surrogatekey from surrogatekeys where id = NEW.id);
于 2014-10-08T18:07:56.057 回答
2

我有同样的问题并通过添加“新”来解决。在字段更新之前。我在这里发布了完整的触发器,以便有人想写一个触发器

DELIMITER $$

USE `nc`$$

CREATE
    TRIGGER `nhachung_province_count_update` BEFORE UPDATE ON `nhachung` 
    FOR EACH ROW BEGIN

    DECLARE slug_province VARCHAR(128);
    DECLARE slug_district VARCHAR(128);

    IF old.status!=new.status THEN  /* neu doi status */
        IF new.status="Y" THEN
            UPDATE province SET `count`=`count`+1 WHERE id = new.district_id;
        ELSE 
            UPDATE province SET `count`=`count`-1 WHERE id = new.district_id;
        END IF;
    ELSEIF old.province_id!=new.province_id THEN /* neu doi province_id + district_id */

        UPDATE province SET `count`=`count`+1 WHERE id = new.province_id; /* province_id */
        UPDATE province SET `count`=`count`-1 WHERE id = old.province_id;
        UPDATE province SET `count`=`count`+1 WHERE id = new.district_id; /* district_id */
        UPDATE province SET `count`=`count`-1 WHERE id = old.district_id;

        SET slug_province = ( SELECT slug FROM province WHERE id= new.province_id LIMIT 0,1 );
        SET slug_district = ( SELECT slug FROM province WHERE id= new.district_id LIMIT 0,1 );
        SET new.prov_dist_url=CONCAT(slug_province, "/", slug_district);

    ELSEIF old.district_id!=new.district_id THEN 

        UPDATE province SET `count`=`count`+1 WHERE id = new.district_id;
        UPDATE province SET `count`=`count`-1 WHERE id = old.district_id;

        SET slug_province = ( SELECT slug FROM province WHERE id= new.province_id LIMIT 0,1 );
        SET slug_district = ( SELECT slug FROM province WHERE id= new.district_id LIMIT 0,1 );
        SET new.prov_dist_url=CONCAT(slug_province, "/", slug_district);

    END IF;


    END;
$$

DELIMITER ;

希望这对某人有帮助

于 2014-07-02T15:24:09.493 回答
2

@gerrit_hoekstra 写道:“但是,自动增量字段的值仅适用于“AFTER-INSERT”触发器 - 在 BEFORE 情况下默认为 0。”

这是正确的,但您可以很容易地选择将由后续 INSERT 插入的自动增量字段值。这是一个有效的例子:

CREATE DEFINER = CURRENT_USER TRIGGER `lgffin`.`variable_BEFORE_INSERT` BEFORE INSERT 
ON `variable` FOR EACH ROW
BEGIN
    SET NEW.prefixed_id = CONCAT(NEW.fixed_variable, (SELECT `AUTO_INCREMENT`
                                                FROM  INFORMATION_SCHEMA.TABLES
                                                WHERE TABLE_SCHEMA = 'lgffin'
                                                AND   TABLE_NAME   = 'variable'));      
END
于 2019-03-11T18:24:10.630 回答