SQL Server, how to detect if the values of a column have changed


Hi Guys,
Welcome back!

Today we have a little insight into the world of triggers.
We will see in which ways to detect if the values ​​of a column have changed

Ready? Go!


The UPDATE function

In a trigger, the UPDATE (FIELD) function is the fastest way to check whether the value of the <FIELD> field has changed.

It is faster because it only parses the T-SQL command to be executed even if the field value is not actually changed.

Example:

       

UPDATE TABELLA SET FIELD = FIELD

       
 
always return TRUE


Reading the tables INSERTED and DELETED


Alternatively, to check if the value of a field has been changed, you can query the two tables INSERTED and DELETED by putting them in JOIN:

       

IF EXISTS( SELECT I.ID FROM INSERTED I JOIN DELETED D ON I.ID = D.ID WHERE I.FIELD <> D.FIELD)
BEGIN

END 
       
 




That's all for Today!
Luca











You may also be interested in:

Previuos post:

Comments

I Post più popolari

SQL Server, execution plan and the lazy spool (clearly explained)

SQL Server, datetime vs. datetime2

La clausola NOLOCK. Approfondiamo e facciamo chiarezza!