2012-02-02 155 views
2

我試圖建立一個MySQL觸發數據插入/在ODBC更新後運行遞歸MySql的觸發不工作

我的觸發低於:

CREATE TRIGGER `myTrigger` 
AFTER INSERT ON `testTable` FOR EACH ROW UPDATE `testTable` SET `Field One` = CONCAT(`Field One`, ' - Trigger'); 

我的觸發聲明MySQL工作臺OK運行但是當我從Microsoft Access數據庫連接到testTable,然後嘗試插入一行我得到的其中之一:

Access Error

爲了讓事情更有趣,當我嘗試運行以下命令:

INSERT INTO `testTable` (`Field One`, `Field Two`, `Field Three`, `Field Four`, `Field Five`) VALUES ('x', 'xx', 'xxx', 'xxxx', 'xxxxx') 

我收到以下錯誤:

ERROR 1442: Can't update table 'testtable' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.

從工作臺我Triggers標籤完整的觸發代碼如下:

-- Trigger DDL Statements 
DELIMITER $$ 

USE `FooBar`$$ 

CREATE 
DEFINER=`JMK`@`%` 
TRIGGER `myTrigger` 
AFTER INSERT ON `testtable` 
FOR EACH ROW 
UPDATE `testTable` SET `Field One` = CONCAT(`Field One`, ' - Trigger')$$ 

我在做什麼錯?

感謝

回答

3

即使添加觸發器的查詢是語法正確的和被處決,這並不意味着觸發功能是有效的。

您的觸發器觸發自己。這是從MySQL documentation

  • Stored functions cannot be used recursively.
  • Within a stored function or trigger, it is not permitted to modify a table that is already being used (for reading or writing) by the statement that invoked the function or trigger.
+0

Aaah,所以我的更新觸發器觸發更新,並在循環中進行? – JMK 2012-02-02 17:40:15

+0

@JMK,是的。這就是發生的事情。 – 2012-02-02 17:43:41

+1

好的,有沒有辦法創建插入或更新後運行的觸發器,但不落入此陷阱? – JMK 2012-02-02 17:47:13