Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SQL 2008 R2 send email when criteria met

Posted on 2013-01-31
8
Medium Priority
?
539 Views
Last Modified: 2013-02-07
I have the below query results.  I need sql to run this query and then send out an email to all users who have been idle for over 90 minutes.

User             Idle Time
bsmith          45
jjones            95
blong             125

So, in the above example i would like an email sent out to jjones@company.com and blong@company.com.
0
Comment
Question by:jnikodym
  • 4
  • 3
8 Comments
 
LVL 23

Expert Comment

by:Steve Wales
ID: 38841589
All nicely documented with screen shots / pictures here:
http://blog.sqlauthority.com/2008/08/23/sql-server-2008-configure-database-mail-send-email-from-sql-database/

You can use that basis to setup your query and send email when you have users breaking your idle time criteria
0
 
LVL 10

Expert Comment

by:deviprasadg
ID: 38842454
Filter the results of the query  with the users with idle time over 90 minutes, and save them to a temp table
to do this add  to where clause

[Idle Time] > 90



Use Procedure sp_send_dbmail to send the mail for all the records in the temp table.
(Also described in the above link)

Also refer: http://msdn.microsoft.com/en-us/library/ms190307.aspx
0
 

Author Comment

by:jnikodym
ID: 38844083
sjwales, I ran through all the steps that you suggested.  I'm getting the error "Could not find stored procedure 'sp_send_dbmail'.
0
Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 

Author Comment

by:jnikodym
ID: 38844097
never mind on that, i fixed the issue, it was just a typo on my part.  My next question is how do i get my query into a stored procedure that will send the email based on my criteria.
0
 

Author Comment

by:jnikodym
ID: 38844248
I have the below code that will email me the results of my query in an attached text file.  What i would like is for each user to be emailed if their idle_minutes are over 90.
EXEC msdb.dbo.sp_send_dbmail
    @profile_name = 'SQLMail',
    @recipients = 'jason@abc.com',
    @query = 'SELECT 

A.USERID, 
LOGINDAT + LOGINTIM LOGIN_DATE_TIME, 
DATEDIFF(mi, P.last_batch, GETDATE()) Idle_Minutes
FROM DYNAMICS..ACTIVITY A 
LEFT JOIN DYNAMICS..SY01400 U ON A.USERID = U.USERID 
LEFT JOIN DYNAMICS..SY01500 C ON A.CMPNYNAM = C.CMPNYNAM 
LEFT JOIN tempdb..DEX_SESSION S ON A.SQLSESID = S.session_id 
LEFT JOIN master..sysprocesses P ON S.sqlsvr_spid = P.spid AND ecid = 0 
LEFT JOIN master..sysdatabases D ON P.dbid = D.dbid
order by Idle_Minutes' ,
    @subject = 'Dynamics GP Idle Times',
    @attach_query_result_as_file = 1 ;

Open in new window

0
 
LVL 23

Expert Comment

by:Steve Wales
ID: 38844525
I guess the big question is, do you have a way to tie a username to an email address ?
0
 

Author Comment

by:jnikodym
ID: 38844812
yes, the userid in the query is the first part of the email address.  So, if userid is jsmith, the email address is jsmith@abc.com
0
 
LVL 23

Accepted Solution

by:
Steve Wales earned 2000 total points
ID: 38851220
You'd want to build a concatenated list of the userid's / email addresses from your query.

So you could do something like this:

(I haven't tested  all of this.  I tested the string build bit but have plugged your query into here - if there are errors, I'm hoping that this is enough to get you started).

declare @emails varchar(max)
set @email = null

select @emails = coalesce(@emails + '; ','') + name + '@abc.com'
FROM DYNAMICS..ACTIVITY A 
LEFT JOIN DYNAMICS..SY01400 U ON A.USERID = U.USERID 
LEFT JOIN DYNAMICS..SY01500 C ON A.CMPNYNAM = C.CMPNYNAM 
LEFT JOIN tempdb..DEX_SESSION S ON A.SQLSESID = S.session_id 
LEFT JOIN master..sysprocesses P ON S.sqlsvr_spid = P.spid AND ecid = 0 
LEFT JOIN master..sysdatabases D ON P.dbid = D.dbid
order by Idle_Minutes

EXEC msdb.dbo.sp_send_dbmail
    @profile_name = 'SQLMail',
    @blind_copy_recipients = @emails,
    @query = 'SELECT 

A.USERID, 
LOGINDAT + LOGINTIM LOGIN_DATE_TIME, 
DATEDIFF(mi, P.last_batch, GETDATE()) Idle_Minutes
FROM DYNAMICS..ACTIVITY A 
LEFT JOIN DYNAMICS..SY01400 U ON A.USERID = U.USERID 
LEFT JOIN DYNAMICS..SY01500 C ON A.CMPNYNAM = C.CMPNYNAM 
LEFT JOIN tempdb..DEX_SESSION S ON A.SQLSESID = S.session_id 
LEFT JOIN master..sysprocesses P ON S.sqlsvr_spid = P.spid AND ecid = 0 
LEFT JOIN master..sysdatabases D ON P.dbid = D.dbid
order by Idle_Minutes' ,
    @subject = 'Dynamics GP Idle Times',
    @attach_query_result_as_file = 1 ;

Open in new window


I will assume you don't necessarily want everyone on the email to see who else it was sent to, so:

From the documentation: http://msdn.microsoft.com/en-us/library/ms190307.aspx

"[ @blind_copy_recipients= ] 'blind_copy_recipients'

    Is a semicolon-delimited list of e-mail addresses to blind carbon copy the message to. The blind copy recipients list is of type varchar(max). Although this parameter is optional, at least one of @recipients, @copy_recipients, or @blind_copy_recipients must be specified, or sp_send_dbmail returns an error."


You probably also want to filter on idle > 90 minutes in there somewhere too
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question