• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 985
  • Last Modified:

SQL trigger code to send an email to a recipient

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
sxxgupta
Asked:
sxxgupta
  • 6
  • 4
1 Solution
 
ste5anSenior DeveloperCommented:
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
 
lcohanDatabase AnalystCommented:
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
 
sxxguptaAuthor Commented:
Almost there:

What is @profile_name or where do I get that from
0
Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 
lcohanDatabase AnalystCommented:
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
 
sxxguptaAuthor Commented:
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
 
lcohanDatabase AnalystCommented:
<<
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
 
sxxguptaAuthor Commented:
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
 
sxxguptaAuthor Commented:
ok, running the trigger did not give errors.   I ran your original code posting.  Waiting for the email now................
0
 
lcohanDatabase AnalystCommented:
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
 
sxxguptaAuthor Commented:
select * from msdb.dbo.sysmail_sentitems;


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

thx.

Sanj
0
 
sxxguptaAuthor Commented:
hahahaha

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

Thanks very much!!!!!

Sanj
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now