Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Field Value Comparisons On Update Trigger 2

Status
Not open for further replies.

Robertio

Programmer
Oct 9, 2000
87
GB
On update we create an Audit of the field values that have changed by comparing the new value to the old one, this works fine unless either value is NULL, at which point it regards the null value as the same as the field value, is there a better way of getting round this problem than running a couple or OR statemensts?

Ideal: (But does not pick up NULLs)
IF :OLD.Field1 <> :NEW.Field1 THEN
CreateAuditRecord(oldVal,NewVal)
END IF;

Work Around: (Is there a better way of doing this?)

IF :)OLD.Field1 <> :NEW.Field1)
OR
(:)NEW.Field1 is null) and :)Old.Field1 is not null))
OR
(:)NEW.Field1 is not null) and :)Old.Field1 is null))
THEN
CreateAuditRecord(oldVal,NewVal)
END IF;

Thanks for any advice
 
The problem arises from the fact theat NULL is considered as an unknown value, so as far as Oracle is concerned is cannot make a decision as to whether the values are equal or not. I had the same problem the other day and resolved it in the same manner. I'm not sure if their is a clever way around it. SOL
&quot;If I'm talking c**p, I'm probably p****d&quot;
 

How about using NVL here;

IF (NVL:)OLD.Field1,'xyz') <> NVL:)NEW.Field1,'uvw')
THEN
CreateAuditRecord(oldVal,NewVal)
END IF;

Where;
'xyz' -- unique value that cannot be qual to Field1 or 'uvw'
'uvw' -- unique value that cannot be qual to Field1 or 'xyz'


 
How about to use updating() function?
 
Thanks all :)

IF UPDATING('Field1') THEN
CreateAuditRecord(oldVal,NewVal)
END IF;

Seems to be the simplist solution, just have to go through and change the triggers on the 100 or tables now, oh well, guess I should have asked here before I started :D
 
I didn't realise you could use the updating() function on individual fields. Have a star from me sem! SOL
&quot;If I'm talking c**p, I'm probably p****d&quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top