我是靠谱客的博主 留胡子自行车,这篇文章主要介绍mysql触发器更新new_MySQL触发器为NEW行设置值,并在同一表中更新另一个,现在分享给大家,希望可以做个参考。

bd96500e110b49cbb3cd949968f18be7.png

I have a table that I keep track of fees for a specific item. These fees can change over time so I have two columns (startDate, endDate) with the current set of fees always having an endDate in the far future. I already have a trigger that I use to do some calculations on the new row being entered but what I also want to have happen is if I enter an item that already has an entry I want to set the previous entry's endDate to the day before the new entry's startDate and the new endDate to a predetermined far-away date. Here is the code for what I tried first:

CREATE

DEFINER=`root`@`%`

TRIGGER `im`.`splitBeforeIns`

BEFORE INSERT ON `im`.`split`

FOR EACH ROW

BEGIN

SET NEW.tcPercent = (NEW.tcOfficeFee / NEW.globalFee) * 100 , NEW.proPercent = 100 - NEW.tcPercent, NEW.endDate = 20501231;

UPDATE im.split set endDate = ADDDATE(NEW.startDate, -1) where procKey = NEW.procKey AND endDate = 20501231;

END$$

The error I get is:

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

解决方案

The answer to this might be unwelcome, but it is: You can't do that.

A Trigger can't update another row of the same table as the row, the trigger was called from.

The typical way to do that, is to create a stored procedure, that inserts into/Updates the target table, then updates the other row(s), all in a transaction.

最后

以上就是留胡子自行车最近收集整理的关于mysql触发器更新new_MySQL触发器为NEW行设置值,并在同一表中更新另一个的全部内容,更多相关mysql触发器更新new_MySQL触发器为NEW行设置值内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(117)

评论列表共有 0 条评论

立即
投稿
返回
顶部