Solved

trigger -- check NULLs if value is different on change

Posted on 2014-02-07
8
408 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
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.

 
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
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…

838 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