Solved

Use Trigger to send email on deletion of a record

Posted on 2010-09-02
7
448 Views
Last Modified: 2012-05-10
Hi,

I have a trigger that on inserting a record in to a table sends out an email stating which record has been inserted.

I'd like to do the same when a record is deleted, I think this is even more tricky than the insert and it's something I don't know where to start with!

Here is the INSERT trigger script:
---------------------------
DECLARE @newcountryname nvarchar(150)
DECLARE @recipients varchar(255)
DECLARE @body varchar(4000)
DECLARE @subject varchar(255)
DECLARE @profile_name varchar(255)
DECLARE @country_list nvarchar(4000)
 
SELECT @newcountryname = (SELECT countryName FROM inserted)
 
SELECT @country_list = ISNULL(@country_list + '<br>', '') + A.countryName
FROM country AS A
 
SET @recipients='ken.rainforth@tarweejarabia.com'
SET @body= 'A new country has been added to the system.  Your countries are now:<br><br>' + @country_list
SET @subject = 'New Country Added: ' + @newcountryname
SET @profile_name = 'TarweejMail'
 
EXEC msdb.dbo.sp_send_dbmail @recipients=@recipients, @body=@body, @subject=@subject, @profile_name=@profile_name, @body_format = 'HTML'
---------------------------------

I tried reversing the script, so ON DELETE.....from DELETED etc but I get, obviously, errors left right and centre.

Any help or explication would be much appreciated.

Regards,

Ken
0
Comment
Question by:kenuk110
  • 3
  • 3
7 Comments
 
LVL 19

Expert Comment

by:Bhavesh Shah
ID: 33586999

can u please tell me the error?
0
 
LVL 14

Expert Comment

by:Michael Dyer
ID: 33587015
Why don't you just store the number of records and send an email whenever it changes?  That would cover any adds and deletions at the same time.  Use a procedure like this to get rows and count.

CREATE PROCEDURE dbo.getRowsAndCount_v1
AS
BEGIN
    SET NOCOUNT ON
 
    -- note that the WHERE clause
    -- should be identical here:
 
    SELECT COUNT(*) FROM table WHERE [...]
    SELECT columns FROM table WHERE [...]
END
GO
0
 

Author Comment

by:kenuk110
ID: 33587611
Hi,

Brichsoft,

It's not so much errors 'running' the command, when I change the INSERTS in the script to DELETE I get red lines saying it's not a valid parameter to use. I thought sending the script someone would know it can't just be reversed and maybe point me in the right direction?

Equtrac Michael,

The reason I wanted it like this is more from a customer service point of view, I just wanted to tell the person who the email is going to 'what' has happened rathe than 'something' has happened. Just made it easier for someone to read whats gone on. Your idea though, if I can revise the script to do what you said but still keep it informative would be great.

I'm not an expert on SQL, I got the original script from here (EE) so I don't have the skill just yet to reconfigure or complete what you said and get similar results.

Any further suggestions or help would be appreciated.

Regards,

Ken
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 19

Accepted Solution

by:
Bhavesh Shah earned 500 total points
ID: 33594012
Hi Ken...

Your code is working very well here.
I just create dumy table called Master with masterid & Mastername.

use ur trigger.it worked.

can u share something which can be help to solve your problem.

alter TRIGGER trg_Del

   ON  Master

   AFTER DELETE

AS 

BEGIN

	-- SET NOCOUNT ON added to prevent extra result sets from

	-- interfering with SELECT statements.

	SET NOCOUNT ON;

DECLARE @recipients varchar(255) 

DECLARE @body varchar(4000) 

DECLARE @subject varchar(255) 

DECLARE @profile_name varchar(255) 

DECLARE @ms varchar(255) 

 

SELECT @ms = mastername FROM deleted



SET @recipients='b.shah@direction.biz' 

SET @body= 'A master has been deleted ' + @ms 

SET @subject = 'New Country Added: ' 

SET @profile_name = 'BJS-TEST' 

 

EXEC msdb.dbo.sp_send_dbmail @recipients=@recipients, @body=@body, @subject=@subject, @profile_name=@profile_name, @body_format = 'HTML'



    -- Insert statements for trigger here



END

Open in new window

0
 

Author Comment

by:kenuk110
ID: 33594382
Hi Brichsoft,

I'll try what you have written but I'm not at work until Saturday, I don't have SQL at home, I'll try it as soon as I get in.

The code I had was for an email to be sent when a record was added, it worked great, I got it from this site. I wanted to do the same when it was deleted so I'll try what you mentioned.

Thanks,

Ken
0
 
LVL 19

Expert Comment

by:Bhavesh Shah
ID: 33594573


Ok Ken..

Enjoy ur time.
0
 

Author Closing Comment

by:kenuk110
ID: 33670583
Perfect!

Thank you so much.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Audit has been really one of the more interesting, most useful, yet difficult to maintain topics in the history of SQL Server. In earlier versions of SQL people had very few options for auditing in SQL Server. It typically meant using SQL Trace …
In this article I will describe the Detach & Attach 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 demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

708 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