Solved

SQL trigger to send an email to a person after an update

Posted on 2014-03-27
6
7,904 Views
Last Modified: 2014-03-28
Need help to create a sql trigger code to send an email  with subject heading "Updated SCAR <number> (whatever this new number is after insert) generated"    and message of the body is        "The SCAR for <SCAR_No> for <AreaRequiringCA> has been changed from <AreaRequiringCA>.CurrentValue to <CarriageReturn> <CarriageReturn> to <AreaRequiringCA>.UpdatedValue.

SCAR_No field value (SCAR <number>) is an integer and a unique record identifier (primary key autonumber field) and <AreaRequiringCA> is a nvarchar(max) in design.

I have the code for the initial insert but do not know what to do with the  update for <AreaRequiringCA> field update.

No idea how to do this.  This email will occur like once in two weeks at most.


For a new record insert of a SCAR_No and emailing; here is the code that works fine.  Need one for the update event of the field <AreaRequiringCA> for a already existing SCAR_No.




use QA
go

CREATE TRIGGER [Email_SCARLogCopy] ON  [dbo].[SCARLogCopy]
   AFTER INSERT
AS
BEGIN

declare @number sysname;
set @number = (select SCAR_No from INSERTED);

DECLARE @sqlstr nvarchar(MAX)
SET @sqlstr = (SELECT AreaRequiringCA FROM inserted);
DECLARE @s_sub nvarchar(1000)
set @s_sub = 'New SCAR ' + @number+ ' was generated';
EXEC msdb.dbo.sp_send_dbmail
      @profile_name = 'SG',
      @recipients = 'sg@zl.com',
      @copy_recipients='sg@zl.com',
      @subject = @s_sub,
      @body =  @sqlstr
END
GO
0
Comment
Question by:sxxgupta
[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
  • 2
6 Comments
 
LVL 21

Accepted Solution

by:
Dale Burrell earned 500 total points
ID: 39960638
Firstly your current trigger only works for single record updates - a common mistake when writing a trigger. The database itself handles multi-record inserts and updates and therefore any trigger you write should do also.

Secondly I'm confused about your SCAR_No - on one hand you say its the auto-generated ID for the record, on the other you say that its changed on update. Although you also say that the AreaRequiringCA is the field updated. So I have written the following assuming that the SCAR_No doesn't change. Regardless its unlikely to be perfect first off, but it hopefully gives you a starting point to build on.

CREATE TRIGGER [Email_SCARLogCopy] ON  [dbo].[SCARLogCopy]
   AFTER INSERT, UPDATE
AS 
BEGIN
  set nocount on

  declare @SCAR_No varchar(38), @s_sub nvarchar(1000), @sqlstr nvarchar(MAX)

  declare @updates table (SCAR_No varchar(38), AreaRequiringCA nvarchar(max), AreaRequiringCAOld nvarchar(max), IsInsert bit)

  insert into @Updates (SCAR_No, AreaRequiringCA, AreaRequiringCAOld, IsInsert)
    select I.SCAR_No, I.AreaRequiringCA, D.AreaRequiringCA, case when D.SCAR_No is null then 1 else 0 end
    from Inserted I
    left join Deleted D on D.SCAR_No = T.SCAR_No

  while exists (select 1 from @Updates) begin
    select top 1 @SCAR_No = SCAR_No
      , @sqlstr = case when IsInsert = 1 then AreaRequiringCA else 'The SCAR for ' + SCAR_No + ' for ' + AreaRequiringCA + ' has been changed from ' + AreaRequiringCAOld + ' to ' + char(13) + char(13) + ' to ' + AreaRequiringCA + '.' end
      , @s_sub = case when IsInsert = 1 then 'New SCAR ' + SCAR_No+ ' was generated' else 'Updated SCAR ' + SCAR_No + ' generated' end
    from @Updates
    exec msdb.dbo.sp_send_dbmail
      @profile_name = 'SG',
      @recipients = 'sg@zl.com',
      @copy_recipients='sg@zl.com',
      @subject = @s_sub,
      @body =  @sqlstr
    delete from @Updates where @SCAR_No = SCAR_No
  end
END
GO

Open in new window

0
 
LVL 35

Expert Comment

by:David Todd
ID: 39960753
Hi,

Strongly suggest that you _not_ send the emails from a trigger.

Get the trigger to insert into a work table the relevant id's, and then get a SQL job to every minute or so scan the table for new ids, send the email, delete the id (or better, mark as sent)

If the trigger fires on a whole of table update, the waiting to send emails could be intolerable, and timeout, and the update/insert fail.

HTH
  David
0
 

Author Comment

by:sxxgupta
ID: 39961337
I got this error:

Msg 4104, Level 16, State 1, Procedure Email_SCARLogCopy_Update, Line 12
The multi-part identifier "T.SCAR_No" could not be bound.

There is no T.SCAR_No
0
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
LVL 21

Expert Comment

by:Dale Burrell
ID: 39961352
Should be I.SCAR_No - I couldn't test it, so leaving it to you to debug... just giving you a starting point.
0
 

Author Comment

by:sxxgupta
ID: 39961354
Yeah I noticed that.  Thanks.
0
 

Author Closing Comment

by:sxxgupta
ID: 39961356
Thank you very much
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

749 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