2012-12-03 86 views
1

我得到了以下兩個觸發器,每個觸發器都獨自工作,但它們不在一起。兩個觸發器,我在更新表時遇到錯誤

我該如何讓他們一起工作?他們更新表中的不同字段。

觸發1:

create trigger wys_sk_u after update on `things` 
for each row 
begin 
UPDATE `current` s 
INNER JOIN things u ON s.id_thing = u.id_thing 
INNER JOIN dude_base b ON b.id= s.id 
SET s.`curr_cash` = u.cash1 * b.cash2/ 100; 
end; 
$$ 

觸發器2:

create trigger suma_u after update on `current` 
for each row 
begin 
UPDATE `current` 
SET `mysum` = `curr_cash` + `mysum`; 
end; 
$$ 

第一個應該更新時cash1cash2更新,和curr_cash變化值。 第二次應更新時curr_cash更新,並更改mysum

我得到了以下錯誤,當我編輯桌上的東西:

#1442 - Can't update table 'current' in stored function/trigger because it is already used by statement which invoked this stored function/trigger. 

@edit 增加了新的answear的問題。


如果我想要做這樣的事情:

CREATE TRIGGER total BEFORE UPDATE ON `current` FOR EACH ROW 
BEGIN 
if new.`new_pay_date` <> old.`new_pay_date` 
    SET new.`total_cash` = new.`curr_cash` + new.`total_cash`; 
end if; 
END; 
$$ 

錯誤:

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SET new.`total_cash` = new.`curr_cash` + new.`total_cash`; end if;' at line 4 

這是工作不

if new.`new_pay_date` <> old.`new_pay_date` 
end if; 

但我需要檢查這個,並且只能隨日期更改而更新。

當前表:

curr_cash 
new_pay_date 
id_person 
id_thing 
total_cash 

任何人都可以幫助我嗎?

回答

2

問題出在第二次觸發。嘗試使用BEFORE UPDATE觸發器使用SET語句來修改字段值 -

CREATE TRIGGER suma_u BEFORE UPDATE ON `current` FOR EACH ROW 
BEGIN 
    SET new.`mysum` = new.`curr_cash` + new.`mysum`; 
END; 
+0

這工作,謝謝 – trinny

0

這是不可能的了,其在觸發器創建觸發器更新表。 MySQL上有鎖定機制,所以你不能更新同一個表的那一行觸發的觸發器,它會導致遞歸調用觸發器和無限循環。