13.2 How do I…Use column values within database triggers?
Problem
When I create database triggers, I need to use the values of the columns in the records being inserted, updated, or deleted. There are many times when I need to use both the values of columns before and after the operation. How do I use column values within database triggers?
Technique
The :NEW and :OLD pseudo-records are used to access new and old column values in a row trigger. You can access old column values of the record before the operation is performed by specifying the :OLD qualifier. The :NEW qualifier provides access to new column values of the record after the operation is performed. With an INSERT statement, the old column values are null, whereas with a DELETE statement, the new column values are null. The preceding colon is required when using the qualifiers in the trigger body. The colon is not allowed when using the qualifiers in the optional WHEN and REFERENCING clauses, as demonstrated in Step 3 of How-To 13.1. The REFERENCING option can be used to avoid name conflicts with qualifiers, if you have a table named as NEW or OLD or simply to increase code readability.
Steps
1. Run SQL*Plus and connect as the WAITE user account. Run CHP13_10.SQL, shown in Figure 13.10, which creates the sample table and data used in this How-To.
The sample table created by the script contains three columns and has database triggers created in the steps that follow. A sample record is created so the use of :OLD modifier can also be demonstrated with an existing record. Run the script to create the sample table and its sample record.
2. Run the CHP13_11.SQL file in SQL*Plus, as shown in Figure 13.11. The CREATE TRIGGER statement contained in the file creates a BEFORE UPDATE trigger referencing the new and old column values.
The :NEW and :OLD qualifiers are used to reference the columns in the table. Line 1 presents the required keywords and specifies the name of the trigger. Lines 2 and 3 specify the triggering statement and FOR EACH ROW option. The trigger will fire any time a record in the table is updated. Lines 5 through 10 use the :NEW and :OLD modifiers to display the values of the columns before and after the operation. Lines 5 through 7 display the values of the columns before the operation. Lines 8 through 10 show the values of the record after the operation.
The next step will display the new and old values of the columns in the existing record by executing an update statement on the table.
3. Run the CHP13_12.SQL file in SQL*Plus, as shown in Figure 13.12. The SERVEROUTPUT SQL*Plus variable is set to ON, to display the results of the operation. The UPDATE statement contained in the file modifies the record in the sample table, firing the BEFORE UPDATE trigger. The UPDATE statement modifies two of the three columns in the table. The new value of the third column will still remain the same as its old value.
The output of the operation displays both the new and old values of the column. A new value was even available for the column not modified by the operation.
How It Works
The :NEW qualifier allows the trigger code to reference the new value of columns in the table. The :OLD qualifier provides access to the column values before the operation is performed. Step 1 creates a sample table and record used by the example trigger. Step 2 creates a BEFORE UPDATE trigger displaying the value of the columns in the table both before and after the update. Step 3 executes an UPDATE statement on the table, causing the trigger to fire.
Comments
The :NEW and :OLD qualifiers are necessary to reference column values in triggers. Make sure you use the right value in your trigger. If you use a new value when you should have used an old value, you may see invalid results. The :NEW and :OLD qualifiers cannot be used with LONG and LONG RAW columns. Triggers should not be created depending on the order in which rows will be processed.