Solved

trigger -- check NULLs if value is different on change

Posted on 2014-02-07
8
400 Views
Last Modified: 2014-02-09
I think i figured out a weird behavior for triggers and want to confirm.

I am logging value changes for a table into a log table, and had way too many.  so i added a where clause to check if the inserted table field is the same as the deleted table field.

But then it was missing a lot of changes.  I then modified it to: ISNULL(i.field1,'') != ISNULL(d.field1,'')

This seems to do the trick where the old value was NULL and the new value is something.  Without the ISNULL it was not catching the change.

Can somebody confirm this is the proper way to check for changed values?
0
Comment
Question by:dougfosterNYC
8 Comments
 
LVL 34

Expert Comment

by:Brian Crowe
ID: 39841951
Although this should work you might want to take a look at the UPDATE() function for a more elegant solution.

http://technet.microsoft.com/en-us/library/ms187326.aspx
0
 

Author Comment

by:dougfosterNYC
ID: 39841976
I should have put an example of the code.. I am using the update function..

My question refers to the last line... the WHERE clause..

	
	IF (UPDATE(quest_contact))
	BEGIN
		INSERT INTO dbo.ADM_LOG (table_name, field_name,log_name, wave_id, comp_id, mgmt_id, old_value, new_value) 
			select 'adm_wave_comp', 'quest_contact', 'Quest Contact Change', ti.Wave_id, ti.Comp_ID, ti.wcomp_id, td.quest_contact, ti.quest_contact
			from inserted ti 
			INNER JOIN deleted td 
			on ti.wcomp_id = td.wcomp_id 
			where ISNULL(ti.quest_contact,'') != ISNULL(td.quest_contact,'')
	END

Open in new window

0
 

Author Comment

by:dougfosterNYC
ID: 39841987
So, to be clear.. if td.quest_contact is null, this insert would not run if my WHERE clause didn't have the ISNULL function and was -- ti.quest_contact != td.quest_contact.
0
 
LVL 16

Expert Comment

by:Surendra Nath
ID: 39842058
That seems ok to me...
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 34

Accepted Solution

by:
Brian Crowe earned 500 total points
ID: 39842070
I just ran a quick test and the UPDATE does evaluate as true for a column if it is changed to or from a NULL so your WHERE clause shouldn't be necessary.  There may be some idiosyncrasies about the functionality that I'm missing but you should be able to get away without it.  I put together the test below and it seemed to produce the results expected each time regardless of whether the column was changed to or from NULL.

IF OBJECT_ID('dbo.Person') IS NOT NULL
      DROP TABLE dbo.Person;
GO

CREATE TABLE [dbo].[Person]
(
      ID            INT NULL,
      NAME      VARCHAR(50) NULL
)

INSERT INTO dbo.Person
      ( ID, Name )
VALUES (1, 'Bob'),
      (2, 'Mary'),
      (3, NULL)

IF OBJECT_ID('dbo.trg_Person_UpdateName') IS NOT NULL
      DROP TRIGGER dbo.trg_Person_UpdateName;
GO

CREATE TRIGGER trg_Person_UpdateName
ON dbo.Person
AFTER UPDATE
AS
IF UPDATE(NAME)
BEGIN
      SELECT * FROM inserted
END

UPDATE dbo.Person
SET Name = 'test'
WHERE ID = 2

UPDATE dbo.Person
SET Name = NULL
WHERE ID = 2

UPDATE dbo.Person
SET Name = 'test2'
WHERE ID > 1

UPDATE dbo.Person
SET Name = NULL
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 39842511
Just so that you are aware, the following:
IF (UPDATE(quest_contact))

Does not mean the value in quest_contact was modified.  it simply indicates that it participated in an UPDATE statement.  Whether it actually changed is up to you to find out.
0
 
LVL 69

Expert Comment

by:ScottPletcher
ID: 39842530
I'd suggest using some really odd replacement for NULL rather than '' -- if someone changes '' to NULL or NULL to '' the code above will miss it.

Say, for example, tilde tilde:

ISNULL(ti.quest_contact,'~~') != ISNULL(td.quest_contact,'~~')
0
 

Author Comment

by:dougfosterNYC
ID: 39845335
there is something funny going on... BriCowe, your test does validate the UPDATE, but i guess it doesn't hurt to have the WHERE clause, since it was missing the changes without it in my database....
0

Featured Post

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now