kenuk110
asked on
Use Trigger to send email on deletion of a record
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 @tarweejar abia.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_nam e, @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
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
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_nam
--------------------------
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
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
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
ASKER
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
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
Ok Ken..
Enjoy ur time.
ASKER
Perfect!
Thank you so much.
Thank you so much.
can u please tell me the error?