mysql的触发器不能对本表进行update操作,如下面 的示例,直接报
Can’t update table ‘tbl’ in stored function/trigger because it is already used by statement which invoked this stored function/trigger 错误,
如果你在触发器里面对刚刚插入的数据进行了 insert/update, 则出现这个问题。因为会造成循环的调用.
create trigger test
before update on test
for each row
update test set NEW.updateTime = NOW() where id=NEW.ID;
END
应该使用set操作,而不是在触发器里使用 update,比如
create trigger test
before update on test
for each row
set NEW.updateTime = NOW();
END
若涉及多个表,则可采取声明局部变量的方式,如下所示
create Trigger updateExam
before insert
on hrt_exam_action_examinee for each ROW
BEGIN
declare personName varchar(50);
declare area varchar(50);
declare deptName varchar(200);
declare idCard varchar(50);
declare courseName varchar(100);
select e.fd_name,e.fd_import_info,e.fd_importal_link into personName,area,deptName from sys_org_element e where e.fd_id = NEW.fd_org_id;
select p.fd_login_name into idCard from sys_org_person p where p.fd_id = NEW.fd_org_id;
select ea.fd_describe into courseName from hrt_exam_action ea where ea.fd_id = NEW.fd_action_id;
set NEW.fd_person_name=personName,NEW.fd_area=area,NEW.fd_dept_name=deptName,NEW.fd_id_card=idCard,NEW.fd_course_name=courseName;
end;
delimiter;
若出现ERROR 1406 : Data too long for column Data too long for column ,
首先检查mysql的默认编码,或者在my.ini里找到
sql-mode=”STRICT_TRANS_TABLES,NO_AUTO_Create_USER,NO_ENGINE_SUBSTITUTION”
把其中的STRICT_TRANS_TABLES,去掉,
或者把sql-mode=STRICT_TRANS_TABLES,NO_AUTO_Create_USER,NO_ENGINE_SUBSTITUTION
注释掉,然后重启mysql就ok了