在更新语句后获取 MySQL 触发器中的旧值

2022-01-01 00:00:00 mysql triggers

我有以下 MySQL 触发器

I have the following MySQL Trigger

SET @iUserId = OLD.LastChangedBy; 

IF NOT NEW.LastChangedBy <=> OLD.LastChangedBy THEN
    SET @iUserId = NEW.LastChangedBy;
END IF;

IF NOT NEW.BookingId<=> OLD.BookingId THEN
    INSERT INTO AuditTrail VALUES (UUID(),@iUserId,'UPDATE','Booking', OLD.BookingId,'BookingType ',OLD.BookingType ,NEW.BookingType ,NOW());
END IF;

这被称为 CREATE TRIGGER Booking_AUPD AFTER ON Booking FOR EACH ROW

This is called as CREATE TRIGGER Booking_AUPD AFTER UPDATE ON Booking FOR EACH ROW

我遇到的问题是如何在 AFTER UPDATE 触发器中获取 OLD 字段值?

The problem I have is how can I get the OLD field value in a AFTER UPDATE trigger ?

推荐答案

出现错误的最可能解释

"There is no OLD row in on INSERT trigger"

是您正在执行创建 AFTER INSERT 触发器的语句,而不是创建 AFTER UPDATE 触发器.

is that you are executing a statement that's creating an AFTER INSERT trigger, rather than creating an AFTER UPDATE trigger.

不能从行中引用 OLD 值的原因是该行在 INSERT 之前存在,是因为该行在 INSERT 之前不存在.

The reason that you can't reference OLD values from the row, as the row existed prior to the INSERT, is that the row did not exist prior to the INSERT.

相关文章