Can anyone help me understand when to use :NEW
and :OLD
in PLSQL blocks, I'm finding it very difficult to understand their usage.
NEW and OLD are pseudorecords that the PL/SQL runtime engine creates and populates whenever a row-level trigger fires. OLD and NEW store the original and new values, respectively, of the record being processed by the trigger. They are called pseudorecords because they do not have all properties of PL/SQL records.
You normally use the terms in a trigger using :old to reference the old value and :new to reference the new value. CREATE OR REPLACE TRIGGER Print_salary_changes BEFORE DELETE OR INSERT OR UPDATE ON Emp_tab FOR EACH ROW WHEN (new.
Answer: The PL SQL data types are composite and scalar. The scalar data types can hold single values like Character, Number, Boolean, and DateTime. While the composite data types store more than one value like collection and record.
You normally use the terms in a trigger using :old
to reference the old value and :new
to reference the new value.
Here is an example from the Oracle documentation linked to above
CREATE OR REPLACE TRIGGER Print_salary_changes BEFORE DELETE OR INSERT OR UPDATE ON Emp_tab FOR EACH ROW WHEN (new.Empno > 0) DECLARE sal_diff number; BEGIN sal_diff := :new.sal - :old.sal; dbms_output.put('Old salary: ' || :old.sal); dbms_output.put(' New salary: ' || :new.sal); dbms_output.put_line(' Difference ' || sal_diff); END;
In this example the trigger fires BEFORE DELETE OR INSERT OR UPDATE
:old.sal
will contain the salary prior to the trigger firing and :new.sal
will contain the new value.
:New and :Old Value can be differentiated in DML Statements .
Insert -- :Old = NULL :New= Inserted new value
Update -- :Old = Value present in table before the Update statement Triggered :New = Given new value to Update
Delete -- :Old = Value before deletion :New = NULL
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