I wish to send an email from a Trigger
, on my SQL Server 2008 machine. The data of the email will be, basically, some of the Trigger information.
Can someone provide some simple/sample code on how to do this, please? E.g. what's the system stored procedure called? Etc.
I've not set up any SQL mail and stuff, so I'm guessing it's built in and I can leverage that. But just to be sure: do I need to install any extra software on the server?
If you find yourself needing to send an email automatically upon certain events occurring in SQL Server, you can do this via a trigger. For example, you could automatically send an email when somebody deletes or updates a record from a table, etc.
Sending emails with SQL Server Agent. The most straightforward way to send emails from SQL Server is by creating a new SQL Agent Job and setting SQL to send emails about the job's status. For that, you can use the profile you just created. To set up, you can use another simple Wizard.
you can use varchar as your data type for email column as emails are usually composed of letters, numbers and special characters.
This example sends an e-mail message to a specified person (MaryM) when the titles table changes.
USE pubs
IF EXISTS (SELECT name FROM sysobjects
WHERE name = 'reminder' AND type = 'TR')
DROP TRIGGER reminder
GO
CREATE TRIGGER reminder
ON titles
FOR INSERT, UPDATE, DELETE
AS
EXEC master..xp_sendmail 'MaryM',
'Don''t forget to print a report for the distributors.'
GO
Source : MSDN
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With