2013-02-02 66 views
0

我想在phpmyadmin中插入表table1之前更新兩個表,但是phpmyadmin在創建此觸發器(Error in MySQL syntax)後顯示錯誤。有多個動作的Mysql觸發器

這工作:

CREATE TRIGGER `my_trig` 
BEFORE INSERT ON `table1` 
FOR EACH ROW 
update table2 
set 
attr = attr+NEW.attr 
where table2.id = NEW.id; 

這工作太(不包括第一次觸發,當然):

CREATE TRIGGER `my_trig` 
BEFORE INSERT ON `table1` 
FOR EACH ROW 
update table3 
set 
attr = attr+NEW.attr 
where table3.id = NEW.id; 

但使用BEGIN- update1; update2; -END它不容創建。任何想法如何創建這種類型的觸發器?在此先感謝您的幫助。

+0

舊的,但也許仍然可用:http://stackoverflow.com/questions/1921627/multiple-insert-update-statements-inside-trigger –

回答

2

你有SET語句

attr = attr + NEW.attr, 

修復之後額外的逗號,

CREATE TRIGGER `my_trig` 
BEFORE INSERT ON `table1` 
FOR EACH ROW 
UPDATE table2 
SET  attr = attr + NEW.attr // <=== removed 
WHERE table2.id = NEW.id; 

使用BEGIN-END

DELIMITER $$ 
CREATE TRIGGER `my_trig` 
BEFORE INSERT ON `table1` 
FOR EACH ROW 
BEGIN 
    UPDATE table2 
    SET  attr = attr + NEW.attr 
    WHERE table2.id = NEW.id; 
END $$ 
DELIMITER ;