Solved

SQL 2008 R2 send email when criteria met

Posted on 2013-01-31
8
518 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
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 

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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Maintenance Plan 3 29
SQL Transaction logs 8 27
Filtered index 5 55
Trying to identify overlapping date ranges 5 13
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 …
There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

911 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

16 Experts available now in Live!

Get 1:1 Help Now