Solved

Sql 2008 R2 Trigger - After update on particular field

Posted on 2013-01-18
3
1,027 Views
Last Modified: 2013-01-18
Hi,
I am wanting to send db_mail when my field 'Status' changes value.

My trigger is as below

create TRIGGER [dbo].[SupportCallEmail]
   ON  [dbo].[SupportCall]
   FOR UPDATE
AS
      IF  UPDATE([Status])
            begin
            exec msdb.dbo.sp_send_dbmail
            @profile_name = 'SMTPPROFILE',      -- Get this value from Server configuration
            @recipients = 'SUPPORT@COMPANY.COM',
            @subject = 'Change in Record Statsus',
            @body = 'status updated'
End


The trigger does work but seems to work on any field change, not just the 'Status' field

Any suggestions?

with thanks in advance
James
0
Comment
Question by:jimbohodge71
3 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 38792356
>  IF  UPDATE([Status])

will return true if the column "status" was in the UPDATE statement, it won't tell you if the value actually changed.

so, either you remove the SET Status = xxx from the update statment if the value doesn't change, or you have to consider to check in the code

create TRIGGER [dbo].[SupportCallEmail]
   ON  [dbo].[SupportCall]
   FOR UPDATE
AS
  declare @status_change_count int

  select  @status_change_count = count(*) 
    from INSERTED i
    join DELETED d
       on i.PK_FIELD_OF_SUPPORTCALLTABLE = d.PK_FIELD_OF_SUPPORTCALLTABLE
    where i.Status <> d.Status
 
      IF  @status_change_count  <> 0
            begin
            exec msdb.dbo.sp_send_dbmail
            @profile_name = 'SMTPPROFILE',      -- Get this value from Server configuration
            @recipients = 'SUPPORT@COMPANY.COM',
            @subject = 'Change in Record Statsus',
            @body = 'status updated'
End 

Open in new window


if Status column may be "null", the expression will be a bit different, but let's start with this
0
 
LVL 14

Expert Comment

by:nishant joshi
ID: 38792451
use

COLUMNS_UPDATED()  function as it would check for all column in table.

ref:
http://msdn.microsoft.com/en-us/library/ms186329.aspx

Thanks,
Nishant
0
 

Author Closing Comment

by:jimbohodge71
ID: 38792565
Thanks very much both of you for your feedback, Solution One was spot on and I'm up and running with it now.

Cheers :-)
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

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

828 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