Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

TRIGGER AFTER DELETE question Expand / Collapse
Author
Message
Posted Wednesday, May 06, 2009 1:56 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, October 07, 2013 9:09 AM
Points: 6, Visits: 49
I have a delete trigger but I want to email me the username field value that has been deleted. Is this possible - if so how?

Sample code:


CREATE TRIGGER delUsers
ON db_users
AFTER DELETE
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
EXECUTE
MSDB.dbo.sp_send_dbmail @profile_name = 'Mail Profile 1'
, @recipients = 'me@email.co.uk'
, @copy_recipients = ''
, @blind_copy_recipients = ''
, @SUBJECT = 'User Deleted'
, @body = 'User Deleted'
, @body_format = 'HTML'
, @importance = 'Normal'
, @sensitivity = 'Normal'

END
GO


Post #710862
Posted Wednesday, May 06, 2009 3:35 AM


SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Friday, March 22, 2013 2:43 PM
Points: 27, Visits: 119
CREATE TRIGGER delUsers ON db_users AFTER DELETE
AS
BEGIN
SET NOCOUNT ON
Declare
@User_name varchar(100),
@body varchar(100)
Select @User_name=usrname from deleted
Set @body='User ' +@User_name+ ' Deleted'
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'Mail Profile 1',
@recipients = 'me@email.co.uk',
@body = @body,
@subject ='User Deletion'
END

--Where usrname is the field in your table db_users


..
Post #710901
Posted Wednesday, May 06, 2009 5:52 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, October 07, 2013 9:09 AM
Points: 6, Visits: 49
Thanks a million - works great!
Post #710962
Posted Wednesday, May 06, 2009 8:08 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 12:31 PM
Points: 10,910, Visits: 12,545
Matt Toynbee (5/6/2009)
Thanks a million - works great!


Actually it only works great if you have single row delete. If you have a batch delete occur you will only get one email sent out and you cannot be guaranteed which row will be the one you alerted about.

For example if I run this code:

Delete
From
db_users
Where
UserName Like 'A%'

You will get only 1 email.

In my opinion, you really should stage the deleted data in a table and then have a job that reads that table and sends you an email or multiple emails. Or you could use SSRS and create an email subscription that sends you an email with deleted users in the last N minutes/hours.




Jack Corbett

Applications Developer

Don't let the good be the enemy of the best. -- Paul Fleming

Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
How to Post Performance Problems
Crosstabs and Pivots or How to turn rows into columns Part 1
Crosstabs and Pivots or How to turn rows into columns Part 2
Post #711104
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse