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;
This is called as CREATE TRIGGER Booking_AUPD
AFTER UPDATE ON Booking FOR EACH ROW
The problem I have is how can I get the OLD field value in a AFTER UPDATE trigger ?
An UPDATE trigger can refer to both OLD and NEW transition variables. INSERT.
Within the trigger body, the OLD and NEW keywords enable you to access columns in the rows affected by a trigger. OLD and NEW are MySQL extensions to triggers; they are not case-sensitive. In an INSERT trigger, only NEW. col_name can be used; there is no old row.
In SQL Server, you can create DML triggers that execute code only when a specific column is updated. The trigger still fires, but you can test whether or not a specific column was updated, and then run code only if that column was updated. You can do this by using the UPDATE() function inside your trigger.
The trigger does no rollback or commit. To initiate any rollback, you have to raise an exception. Thus your insert/update/delete command will abort. The rollback or commit action has to be raised around your SQL command.
MySQL AFTER UPDATE triggers are invoked automatically after an update event occurs on the table associated with the triggers. The following shows the syntax of creating a MySQL AFTER UPDATE trigger: CREATE TRIGGER trigger_name AFTER UPDATE ON table_name FOR EACH ROW trigger_body Code language: SQL (Structured Query Language) (sql)
This after_sales_update trigger is automatically fired before an update event occurs for each row in the sales table. If you update the value in the quantity column to a new value the trigger insert a new row to log the changes in the SalesChanges table. First, the name of the trigger is after_sales_update specified in the CREATE TRIGGER clause:
In an UPDATE trigger, you can use OLD.col_name to refer to the columns of a row before it is updated and NEW.col_name to refer to the columns of the row after it is updated. here is my complete code of how trigger will work when some column value updated
First, the name of the trigger is after_sales_update specified in the CREATE TRIGGER clause: Finally, use the IF-THEN statement inside the trigger body to check if the new value is not the same as the old one, then insert the changes into the SalesChanges table:
In an UPDATE TRIGGER
, you can use the OLD
keyword to access the row data which is being replaced by the update. The NEW
keyword allows accessing the incoming row data which will replace the old row, if successful.
An example of an UPDATE
trigger is:
CREATE TRIGGER upd_check AFTER UPDATE ON SomeTable
FOR EACH ROW
BEGIN
IF (OLD.LastChangedBy <> NEW.LastChangedBy) THEN
INSERT INTO AuditSomeTable(ID, LastChangedBy)
VALUES (OLD.ID, OLD.LastChangedBy);
END IF;
END;
SQLFiddle here
Depending on the type of trigger created, the OLD
and NEW
rows may not be available to you:
INSERT TRIGGER
NEW
pseudo rows only.UPDATE TRIGGER
NEW
and OLD
pseudo rowsDELETE TRIGGER
OLD
pseudo rowsi.e. there is no OLD
row on an INSERT
trigger, and no NEW
row on a DELETE
trigger.
OP's Question
OP hasn't provided the actual code, and the error message referred to in the comments:
There is no OLD row in on INSERT trigger
indicates that the OP had inadvertently created an INSERT TRIGGER
and not an UPDATE TRIGGER
as was indicated in the question. An INSERT
trigger has no OLD
pseudo table.
The most likely explanation for getting an error
"There is no OLD row in on INSERT trigger"
is that you are executing a statement that's creating an AFTER INSERT
trigger, rather than creating an AFTER UPDATE
trigger.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With