Solved

trigger -- check NULLs if value is different on change

Posted on 2014-02-07
8
412 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Removing SCCM 2016 4 53
CASE Statement using different fields 8 35
Do not display comma when no last name 8 48
MS SQL TO JSON 14 18
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

710 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