MySQL触发器更新本表数据异常:Can't update table 'tbl' in stored function/trigger because it

如果你在触发器里面对刚刚插入的数据进行了 insert/update, 则出现这个问题。因为会造成循环的调用.

create trigger test
before update on tablename
for each row
  update tablename set NEW.content = '' where id=NEW.ID;
END



应该使用set操作,而不是在触发器里使用 update,比如

create trigger test
before update on tablename
for each row
set NEW.content = '';
END


接下来,根据我的应用进行描述一下:
我的需求是表A插入一条数据,同步到表B。同步之后,更新表A的某个字段。
DELIMITER $$
CREATE
    TRIGGER triggername BEFORE INSERT
    ON tableA
    FOR EACH ROW BEGIN
      insert into tableB(content) values(new.content);
      set new.content='';
    END$$
DELIMITER ;

你可能感兴趣的:(java,mysql)