You are on page 1of 2

NEW and OLD implicit variables in rowlevel triggers

In addition to the local variables declared in the trigger, row-level triggers have access to implicit variables whose values are automatically set by the system. The OLD variable refers to the value of a column before the incident occurs; the NEW variable refers to a column affected by the incident, after it has occurred. You can use expressions to read from and assign values to row variables. Certain operations on the NEW or OLD row variables may not be accessible or modifiable depending on the type of modification. For example, if the ObjectServer deletes a row, there is no NEW row to read or modify. The following table shows when the NEW and OLD variables are available depending on the database operation. Table 1. Availability of special row variables Operation Timing mode BEFORE AFTER BEFORE AFTER BEFORE AFTER Is the NEW variable available? Y Y Y Y N N Y Y Y N Y N N N N N Is the NEW variable modifiable? N N Y N Y Y Y N Is the OLD variable available? N N N N N N Y N Is the OLD variable modifiable?

INSERT INSERT UPDATE UPDATE DELETE DELETE

REINSERT BEFORE REINSERT AFTER

Note: In a post-reinsert trigger, only the NEW variable is available, and this represents the data from the INSERT statement. For example, if Tally is not specified in the INSERT statement, new.Tally will have a default value of 0 (zero).

Example
The following database trigger uses the NEW variable to update the StateChange column when a row in the alerts.status table is modified to time stamp the change.

create trigger SetStateChange group default_triggers priority 1 before update on alerts.status for each row begin set new.StateChange = getdate; end;

Parent topic: Creating database triggers (CREATE TRIGGER command)

You might also like