Home : SQL trigger to email out of range entries
Q14515 - INFO: SQL trigger to email out of range entries

ADVANCED USERS ONLY - NOT COVERED UNDER WIMS SUPPORT AGGREEMENT

Discuss with IT / DBA before attempting to setup. 

Setup SQL Server Express Mail (may not be necessary when running non-express versions of SQL Server):

The following script sets up to use the myAccount@gmail.com account to email messages from SQL Server Express.  You must replace myAccount@gmail.com and the @mailserver_name smpt.gmail.com with your email and smtp server information.  Use SQL Management Studio> New Query and paste in the following:

--Enable Database Mail for Express--
use master
go
sp_configure 'show advanced options',1
go
reconfigure
with override
go
sp_configure 'Database Mail XPs',1
go
reconfigure
go

--Setup SMTP Outgoing Email Account--
EXECUTE msdb.dbo.sysmail_add_account_sp
@account_name ='myAccount',
@email_address = 'myAccount@gmail.com',
@display_name = 'From SQL Server',
@replyto_address = NULL,
@description = 'Mail Account for Database Mail',
@mailserver_name = 'smtp.gmail.com', -- the following fields are part of server definition
@mailserver_type = N'SMTP',
@port = 587,
@username = 'myAccount@gmail.com',
@password = 'myPwd',
@use_default_credentials = 0,
@enable_ssl = 'TRUE',
@account_id = NULL

--Create Mail Profile--
EXECUTE msdb.dbo.sysmail_add_profile_sp
@profile_name = 'myEmailProfile',
@description = 'Profile needed for database mail'

 

--Add Profile Account--
EXECUTE msdb.dbo.sysmail_add_profileaccount_sp
@profile_name = 'myEmailProfile',
@account_name = 'myAccount',
@sequence_number = 1

 

--Add Principle Account--
EXECUTE msdb.dbo.sysmail_add_principalprofile_sp
@profile_name = 'myEmailProfile',
@principal_name = 'public',
@is_default = 1 ;

 

To verify setup, execute the following commands.  Each query should return 1 record.

-- verify setup

select * from msdb.[dbo].[sysmail_account]
select * from msdb.[dbo].[sysmail_profile]
select * from msdb.[dbo].[sysmail_profileaccount]
select * from msdb.[dbo].[sysmail_principalprofile]

Send a test message:

USE [msdb]
GO

EXEC msdb.dbo.sp_send_dbmail

@recipients = 'useremail@xyz.com',

@subject = "Test email from SQL",

@body = "This is a Test"

GO

 

Add OPSDBA user into msdb so the OPSDBA user can execute the sp_send_dbmail. 

In Management Studio:

  • Go to Security>Logins.  Right Click on OPSDBA and go to Properties.
  • Click on User Mapping page.
  • Check Map box for msdb.

 

 

Execute the following statement against msdb:  Grant EXEC on sp_send_dbmail to opsdba

 

Now create the trigger to send email when out of range:

 

In SQL Management Studio, go to Triggers under Datatbl, right click and choose add trigger:

-- Trigger outputs email when Entry Min or Entry Max exceeded
SET ANSI_NULLS ON
GO
SET
QUOTED_IDENTIFIER ON
GO
CREATE
TRIGGER [dbo].[EMAIL_OOR]
ON OPSWWTUTOR.[dbo].[DATATBL]
FOR INSERT, UPDATE

AS
BEGIN

DECLARE @body NVARCHAR(MAX) =(Select CAST(vardesc.varnum AS NVARCHAR) +'-'+ VARDESC.name + ' '+ 'had a value of'+ ' '+ inserted.textvalue +' '+'on'+' '+CONVERT(varchar,DATESTAMP , 101) +' '+'which was outside of the entry range.' from inserted, vardesc where inserted.varid=vardesc.VARID);

IF EXISTS (select VARDESC.name, inserted.curvalue from vardesc, inserted where vardesc.varid=inserted.VARID and (inserted.curvalue<vardesc.entrymin or inserted.curvalue >vardesc.EntryMax))
EXEC msdb.dbo.sp_send_dbmail
@recipients = '5051234567@messaging.sprintpcs.com; xyz@hach.com',
@subject = 'OOR',
@body = @body ;
SET NOCOUNT ON;
END
GO

TECH NOTES AND HINTS:

1. To send text messages (SMS), set recipient to phone number plus provider extension.  Example: 5051234567@txt.att.net.  See http://www.opssys.com/InstantKB/article.aspx?id=11752 for info on other carriers.

2. If editing the trigger change CREATE TRIGGER to ALTER TRIGGER

3. Change OPSWWTUTOR to your facility database in the Create Trigger command.

Related Articles
No Related Articles Available.

Article Attachments
No Attachments Available.

Related External Links
No Related Links Available.
Help us improve this article...
What did you think of this article?

poor 
1
2
3
4
5
6
7
8
9
10

 excellent
Tell us why you rated the content this way. (optional)
 
Approved Comments...
No user comments available for this article.
Created on 8/27/2018 12:08 PM.
Last Modified on 8/9/2024 11:43 AM.
Last Modified by Ryan Rhoten.
Article has been viewed 4809 times.
Rated 0 out of 10 based on 0 votes.
Print Article
Email Article