Solved

SQL 2008 R2 send email when criteria met

Posted on 2013-01-31
8
517 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 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
 

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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

     When we have to pass multiple rows of data to SQL Server, the developers either have to send one row at a time or come up with other workarounds to meet requirements like using XML to pass data, which is complex and tedious to use. There is a …
In this article I will describe the Backup & Restore 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 demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now