SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Duplicate mails from sp_send_dbmail within trigger


Duplicate mails from sp_send_dbmail within trigger

Author
Message
johnnyrmtl
johnnyrmtl
Say Hey Kid
Say Hey Kid (671 reputation)Say Hey Kid (671 reputation)Say Hey Kid (671 reputation)Say Hey Kid (671 reputation)Say Hey Kid (671 reputation)Say Hey Kid (671 reputation)Say Hey Kid (671 reputation)Say Hey Kid (671 reputation)

Group: General Forum Members
Points: 671 Visits: 429
Hello,

I have a trigger on a table and when a record is inserted I receive the email 3 times.
I did a quick search and haven't found anything to resolve the issue. any ideas ? as this is my trigger.

USE [ABC]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER [dbo].[table1]
ON [dbo].[table1]
FOR INSERT, UPDATE, DELETE

AS
BEGIN
DECLARE @EmailTXT varchar(max)
IF (Select Count(*) from Inserted) > 0
BEGIN
-- inserted


Select @EmailTXT = srvname + ' ' + name + ' ' from inserted

EXEC msdb.dbo.sp_send_dbmail @profile_name = 'DBA Alert',
@recipients = 'emailhidden',
@subject = 'A record has been inserted or modified on table blabla on server. Please Verify',
@body_format = 'HTML',
@importance = 'High',
@body = @EmailTXT

END
ELSE If (Select Count(*) from Deleted) > 0
BEGIN

-- deleted


Select @EmailTXT = srvname + ' ' + name + ' ' from deleted

EXEC msdb.dbo.sp_send_dbmail @profile_name = 'DBA Alert',
@recipients = email hidden,
@subject = 'A record has been deleted on table blabla on server. Please Verify',
@body_format = 'HTML',
@importance = 'High',
@body = @EmailTXT




END
END
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search