我得到了以下两个触发器,每个触发器都单独工作,但在一起时却不起作用。
如何让它们协同工作?他们更新表中的不同字段。
触发器 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;
$$
第一个应该在 cash1
或 cash2
更新时更新,并更改 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.
@编辑 为问题添加了新的答案。
如果我想做这样的事情怎么办:
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
谁能帮我解决这个问题?
最佳答案
问题出在第二个触发器中。尝试使用 BEFORE UPDATE 触发器使用 SET 语句更改字段值 -
CREATE TRIGGER suma_u BEFORE UPDATE ON `current` FOR EACH ROW
BEGIN
SET new.`mysum` = new.`curr_cash` + new.`mysum`;
END;
关于mysql - 两个触发器,更新表时出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13684202/