MySql错误:无法更新存储函数/触发器中的表,因为它已被调用此存储函数/触发器的语句使用

时间:2013-03-08 18:19:22

标签: mysql triggers

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

这个错误是什么意思?

5 个答案:

答案 0 :(得分:69)

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

但是,根据您要实现的目标,您可以使用NEW.fieldname访问新值,甚至可以使用旧值(如果使用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

答案 1 :(得分:5)

正确的语法是:

FOR EACH ROW SET NEW.bname = CONCAT( UCASE( LEFT( NEW.bname, 1 ) )
                                   , LCASE( SUBSTRING( NEW.bname, 2 ) ) )

答案 2 :(得分:4)

A" BEFORE-INSERT" -trigger是在插入上实现相同表更新的唯一方法,并且只能从MySQL 5.5+实现。但是,自动增量字段的值仅适用于" AFTER-INSERT"触发器 - 在BEFORE-case中默认为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);

答案 3 :(得分:3)

我有同样的问题并通过添加" new修复。"在更新字段之前。我在这里发布完全触发器,以便某人想要编写触发器

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 ;

希望这有助于某人

答案 4 :(得分:0)

@gerrit_hoekstra写道:“但是,自动递增字段的值仅可用于“ AFTER-INSERT”触发器-在之前的情况下默认为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