MySQL触发器 重要学习资料:
http://dev.mysql.com/doc/refman/5.1/zh/triggers.html
在使用触发器的时候,遇到一个错误:
[SQL]insert into tb_recharge_records(user_id,pointcoupon_added) values(214983,10); [Err] 1442 - Can't update table 'tb_recharge_records' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
通过查找资料:
http://blog.csdn.net/liuliu20036/article/details/4158891
触发器在插入时更新此条新记录 错误it is already used by statement which invoked this stored functiontrigger的解决方法
问题:
create trigger InsertUser
begin
update users
set user_power='[resource]'
where user_id=new.user_id;
end;
当触发器执行过程中出现了错误:我使用的是Mysql5.0
Can't update table 'users' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
请问这里是怎么回事?
解决方法:
如果你在触发器里面对刚刚插入的数据进行了 insert/update, 则出现这个问题。因为会造成循环的调用.
应该使用set操作,而不是在触发器里使用 update,比如
create trigger InsertUser
begin
set new.user_power='[resource]'
end;
另外这里必须在插入之前来执行该触发器。
============
通过触发器 实现添加记录,则自动添加插入时间
set @newid=NEW.recharge_id;
if @newid>0 then
update tb_recharge_records set add_time=now() where recharge_id=@newid and ""="触发器不可调用宿主表";
end if;