zl程序教程

您现在的位置是:首页 >  数据库

当前栏目

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

mysqlIT异常数据 in 更新 &# 39
2023-09-14 08:59:34 时间
MySQL触发器更新本表数据异常:Can't update table 'tbl' in stored function/trigger because it

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

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



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

Java代码  收藏代码
create trigger test  
before update on tablename  
for each row  
set NEW.content = '';  
END  


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