OLD Context Variables
Go Up to Procedures and Triggers
Indicates a current column value in an UPDATE
or DELETE
operation. Available in triggers only.
OLD.column
Argument | Description |
---|---|
<column> |
Name of a column in the affected row |
Description: Triggers support two context variables: OLD
and NEW
. An OLD
context variable refers to the current or previous value of a column in an INSERT
or UPDATE
operation.
Context variables are often used to compare the values of a column before and after it is modified. Context variables can be used anywhere a regular variable can be used.
Example: The following script is a trigger that fires after the EMPLOYEE
table is updated, and compares an employee’s old and new salary. If there is a change in salary, the trigger inserts an entry in the SALARY_HISTORY
table.
CREATE TRIGGER SAVE_SALARY_CHANGE FOR EMPLOYEE
AFTER UPDATE AS
BEGIN
IF (OLD.SALARY <> NEW.SALARY) THEN
INSERT INTO SALARY_HISTORY
(EMP_NO, CHANGE_DATE, UPDATER_ID, OLD_SALARY, PERCENT_CHANGE)
VALUES (OLD.EMP_NO, ‘NOW’, USER, OLD.SALARY,
(NEW.SALARY - OLD.SALARY) * 100 / OLD.SALARY);
END ;
See Also
For more information about creating triggers, see CREATE TRIGGER.