Solved

SQL trigger code to send an email to a recipient

Posted on 2014-03-26
11
914 Views
Last Modified: 2014-03-26
Need help to create a sql trigger code to send an email to    test@gmail.com     with subject heading "New SCAR <number> (whatever this new number is after insert) generated"    and message of the body is        "The SCAR is for <AreaRequiringCA>

<number> is the SCAR_No field value for a new insert and <AreaRequringCA> is a ntext in design.

So I would like the message to go only after an insert of a new SCAR_No has been created and the <AreaRequirintgCA> goes from null to some ntext value.  The table is called "SCARLogCopy"

No idea how to do this.  This email will occur like once in two weeks at most.
1.gif
0
Comment
Question by:sxxgupta
  • 6
  • 4
11 Comments
 
LVL 32

Expert Comment

by:ste5an
ID: 39957019
You should not do that. While you can use database mail in a trigger, you need to consider three things:

1. E-mail is unreliable and asynchronous. So sending it in a trigger makes no sense.
2. The trigger may lock due to longer execution time.
3. The trigger may rollback due to errors in the sending process.

Use a separate process instead. E.g. SQL Server Agent or a scheduled task or your own Windows Service.
0
 
LVL 39

Expert Comment

by:lcohan
ID: 39957040
It would be something like:

CREATE TRIGGER [TI_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 AreaRequringCA FROM inserted);
DECLARE @s_sub nvarchar(1000)
set @s_sub = 'New SCAR ' + @number+ ' was generated';
EXEC msdb.dbo.sp_send_dbmail
      @profile_name = 'yourmailprofile',
      @recipients = 'you@email.com',
      @subject = @s_sub,
      @body =  @sqlstr
END
GO

Sorry - that was for update. Use this one instead for your specific request
0
 

Author Comment

by:sxxgupta
ID: 39957205
Almost there:

What is @profile_name or where do I get that from
0
 
LVL 39

Expert Comment

by:lcohan
ID: 39957225
You need to configure database mail on that server and you can do that via SSMS.
If it's already configured you should be able to see that in the same wizard or just run query below to list profiles on that SQL Server.

exec msdb.dbo.sysmail_help_profile_sp;


After you get the profile name you want to use just replace that string with your profile name and the actual email addresses separated by ; in that @recipients  string.
0
 

Author Comment

by:sxxgupta
ID: 39957251
ok did that

now when I run the trigger I get:

Msg 208, Level 16, State 6, Procedure TI_SCARLogCopy, Line 14
Invalid object name 'dbo.TI_SCARLogCopy'.

and faulting at the line

EXEC msdb.dbo.sp_send_dbmail
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 39

Expert Comment

by:lcohan
ID: 39957277
<<
now when I run the trigger I get:

Msg 208, Level 16, State 6, Procedure TI_SCARLogCopy, Line 14
Invalid object name 'dbo.TI_SCARLogCopy'.
>>


Did you selected the appropriate database ?

Did you executed the CREATE TRIGGER or ALTER TRIGGER? From what I see I believe you RAN either an ALTER and trigger is not there or ALTER in a different database.

OR...check is the name of the trigger was not between square brackets because then its name would be [dbo.TI_SCARLogCopy] instead of dbo.[TI_SCARLogCopy]
0
 

Author Comment

by:sxxgupta
ID: 39957302
I executed this:

USE [QA]
GO
/****** Object:  Trigger [dbo].[TI_SCARLogCopy]    Script Date: 03/26/2014 16:04:16 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER TRIGGER [dbo].[TI_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 = 'SanjayGupta',
      @recipients = 'sgupta@zoll.com',
      @subject = @s_sub,
      @body =  @sqlstr
END


and got the same error......sorry I am new to this.  Do I need to first create the trigger then alter it.......
0
 

Author Comment

by:sxxgupta
ID: 39957344
ok, running the trigger did not give errors.   I ran your original code posting.  Waiting for the email now................
0
 
LVL 39

Accepted Solution

by:
lcohan earned 500 total points
ID: 39957408
You can check SQL mail sent items by:

select * from msdb..sysmail_sentitems;

and unsent mail by:

select * from msdb..sysmail_unsentitems


If the mail was SENT by SQL and you do not get it then you need to check with someone that is taking care of the SMPT server.
0
 

Author Comment

by:sxxgupta
ID: 39957559
select * from msdb.dbo.sysmail_sentitems;


ran this with no records returned......so no mail was sent, correct?

thx.

Sanj
0
 

Author Comment

by:sxxgupta
ID: 39957574
hahahaha

SMTP server address was wrong.  Corrected and email came thru.

Thanks very much!!!!!

Sanj
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
In this article I will describe the Backup & Restore 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.
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.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

743 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now