Send Email from Trigger in SQL Server

Submitted by:adam Smith

Date added:08 September, 2018

Category:ASP

Whenever a row is inserted in the Customers Table, the following trigger will be executed.

Tags: trigger

Code Snippet:

CREATE TRIGGER [dbo].[Customer_INSERT_Notification]
ON [dbo].[Customers]
AFTER INSERT
AS
BEGIN
SET NOCOUNT ON;

DECLARE @CustomerId INT

SELECT @CustomerId = INSERTED.CustomerId
FROM INSERTED
declare @body varchar(500) = 'Customer with ID: ' + CAST(@CustomerId AS VARCHAR(5)) + ' inserted.'
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'Mudassar_Email_Profile'
,@recipients = 'recipient@gmail.com'
,@subject = 'New Customer Record'
,@body = @body
,@importance ='HIGH'
END
 
 

Comments