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

Get OLD Value in MySQL Trigger AFTER Update Statement(在更新语句后获取 MySQL 触发器中的旧值)
本文介绍了在更新语句后获取 MySQL 触发器中的旧值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 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.

这篇关于在更新语句后获取 MySQL 触发器中的旧值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)
SQL/MySQL: split a quantity value into multiple rows by date(SQL/MySQL:按日期将数量值拆分为多行)