Solved

SQL 2008 R2 send email when criteria met

Posted on 2013-01-31
8
522 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Insert parts by customer 12 51
sql query help 15 50
How can I find this data? 3 23
Finding Where Clause Value in SQL Views and SP 21 29
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…
Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…

679 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