Link to home
Start Free TrialLog in
Avatar of sakthikumar
sakthikumar

asked on

How to find out which column has changed from a trigger.

I want to find out all the columns that are changed, and I need to store
the old and new values for the changed columns.

Is there anyway, I can dynamically find out the changed columns.
or I need to compare the old and new values for each column?
Avatar of slightwv (䄆 Netminder)
slightwv (䄆 Netminder)

>>I need to compare the old and new values for each column?

This way.
you'll have to examine each of them individually.  there is no "list changed" function

something like this for each column


if (:new.col1 != :old.col1) or (:new.col1 is not null and :old.col1 is null) or (:new.col1 is null and :old.col2 is not null) then
   dbms_output.put_line('col1 changed');
end if;
note,  if your column is constrained to be NOT NULL then you can remove the isnull/isnotnull checks
- you can create a so called audit table then store the :old and :new in your trigger as shown in the following sample:
http://www.techonthenet.com/oracle/triggers/after_update.php
>>if (:new.col1 != :old.col1) or (:new.col1 is not null and :old.col1 is null)

I would probably go with:
nvl(:new.col1,'---!!!---') != nvl(:old.col1,'---!!!---')

Where '---!!!---' is some value that WOULD NEVER exist in the column.  Of course you would need to account for the same data type.
ASKER CERTIFIED SOLUTION
Avatar of Sean Stuber
Sean Stuber

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial