Solved

SQL 2008 R2 send email when criteria met

Posted on 2013-01-31
8
527 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 22

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
Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

 

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 22

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 22

Accepted Solution

by:
Steve Wales earned 500 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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Audit has been really one of the more interesting, most useful, yet difficult to maintain topics in the history of SQL Server. In earlier versions of SQL people had very few options for auditing in SQL Server. It typically meant using SQL Trace …
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.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

734 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