Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

trigger -- check NULLs if value is different on change

Posted on 2014-02-07
8
Medium Priority
?
418 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
Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

 
LVL 16

Expert Comment

by:Surendra Nath
ID: 39842058
That seems ok to me...
0
 
LVL 34

Accepted Solution

by:
Brian Crowe earned 2000 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 70

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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

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…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

916 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