我寫了一個觸發器,它的作用是在插入數據前,如果列name的字符串中含有”-”(中劃線),則替換為” “(空格)。
錯誤的代碼:
delimiter |
create trigger replaceGangcn
before insert on dept
for each row
begin
update dept set name = replace(new.name,’-',’ ‘);
end
|
delimiter ;
執行後,觸發器成功使用到表dept上,但是當我向表中插入數據時,MySQL卻提示:
ERROR 1442 (HY000): Can’t update table ‘dept’ in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
不是很明白這個錯誤信息的含義。不過我知道了一點,MySQL的觸發器在處理本表的數據時只需使用New.列名直接操作即可,而不要用update聲明。上面那段代碼改成如下這樣即可:
delimiter |
create trigger replaceGangcn
before insert on dept
for each row
begin
set new.name = replace(new.name,’-',’ ‘);
end
|
delimiter ;