Solved

trigger -- check NULLs if value is different on change

Posted on 2014-02-07
8
406 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
LVL 16

Expert Comment

by:Surendra Nath
ID: 39842058
That seems ok to me...
0
 
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:Scott Pletcher
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Tsql query 6 22
Microsoft Access Write errors seem to be caused by bit fields 4 36
Create snapshot on MSSQL 2012 3 20
syntax sql error 2 14
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

803 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