Solved

Sql 2008 R2 Trigger - After update on particular field

Posted on 2013-01-18
3
1,041 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
[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
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

Industry Leaders: 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!

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
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.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

691 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