Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get OLD Value in MySQL Trigger AFTER Update Statement

Tags:

mysql

triggers

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 ?

like image 964
neildt Avatar asked Jul 16 '13 15:07

neildt


People also ask

Does an on update trigger have access to old and new variables?

An UPDATE trigger can refer to both OLD and NEW transition variables. INSERT.

What is new and old in MySQL trigger?

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.

How do you execute a trigger only when a specific column is updated MySQL?

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.

How do I rollback a trigger in MySQL?

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.

How do I create a MySQL after update trigger?

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)

How does the after_sales_update trigger work?

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:

How to use old and New cols in an update trigger?

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

How do I change the value of a sales update trigger?

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:


2 Answers

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

  • Access to the NEW pseudo rows only.

UPDATE TRIGGER

  • Access to the NEW and OLD pseudo rows

DELETE TRIGGER

  • Access only to the OLD pseudo rows

i.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.

like image 192
StuartLC Avatar answered Oct 12 '22 22:10

StuartLC


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.

like image 44
spencer7593 Avatar answered Oct 12 '22 22:10

spencer7593