Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a SQL Server Trigger send me an email?

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?

like image 900
Pure.Krome Avatar asked Nov 23 '09 00:11

Pure.Krome


People also ask

Can SQL trigger email?

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.

Can SQL Server Agent send emails?

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.

Does SQL have an email data type?

you can use varchar as your data type for email column as emails are usually composed of letters, numbers and special characters.


1 Answers

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

like image 156
Pratik Avatar answered Oct 18 '22 08:10

Pratik