Solved

Advice: Open a page or run a query every day

Posted on 2009-07-11
9
220 Views
Last Modified: 2012-05-07
Hi,

I would like to send an email to one or more people depending on a query, I can do this via an ASP page, but I would like to automate this so this runs everyday, can this be done via SQL Management Studio - either open a an ASP page or within the programme itself?

Thanks!
0
Comment
Question by:sanjshah12
9 Comments
 

Accepted Solution

by:
KoreKailas earned 100 total points
ID: 24830212
First you need to configure the mail profile with SQL Server using that account can send one or multiple receipants. This you can achive using writing procedure.
# Configure E-Mail
       - sysmail_add_account_sp
       - sysmail_add_profile_sp
       - sysmail_add_profileaccount_sp
       - sysmail_add_principalprofile_sp
# write procedure to pick the receipants

I hope you will get it code sample in SQL Server Books Online. Please let us know if you need the script.
0
 
LVL 60

Assisted Solution

by:chapmandew
chapmandew earned 200 total points
ID: 24830350
Sure, first read my article where I show you how to setup db mail in sql 2005.

http://articles.techrepublic.com.com/5100-10878_11-6161839.html
http://articles.techrepublic.com.com/5100-10878_11-6164310.html

Then, you can setup a sql agent job to send out the email as often as you need.
http://msdn.microsoft.com/en-us/library/ms181153.aspx
0
 

Author Comment

by:sanjshah12
ID: 24830416
Thanks chaps, I looked at Tim's articles (very nice!), where you have @recipients could I set these via a query,

select selectedemails= useremail From dbo.tbl_login where (status = "" and ReportDate < Date)

@recipients = selectedemails

Regards,

0
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 
LVL 60

Assisted Solution

by:chapmandew
chapmandew earned 200 total points
ID: 24830608
sure, you could...would need to do it like this though...

declare @recipients nvarchar(max)

select @recipients = isnull(@recipients,'') + useremail + ';'
from dbo.tbl_login where (status = "" and ReportDate < Date)
 

then you can pass that variable in.
0
 

Author Comment

by:sanjshah12
ID: 24835036
Tim,

Just to be clear (for me), I will run the following (which must be used on the msdb):

USE msdb
GO
DECLARE @ProfileName VARCHAR(255)
DECLARE @AccountName VARCHAR(255)
DECLARE @SMTPAddress VARCHAR(255)
DECLARE @EmailAddress VARCHAR(128)
DECLARE @DisplayUser VARCHAR(128)

SET @ProfileName = 'Send Reminder Emails';
SET @AccountName = 'DBMailAccount';
SET @SMTPAddress = 'smtp.mydomain.com';
SET @EmailAddress = 'myuser@mydomain.com';
SET @DisplayUser = 'support@mydomain.com';

EXECUTE msdb.dbo.sysmail_add_account_sp
@account_name = @AccountName,
@email_address = @EmailAddress,
@display_name = @DisplayUser,
@mailserver_name = @SMTPAddress

EXECUTE msdb.dbo.sysmail_add_profile_sp
@profile_name = @ProfileName

EXECUTE msdb.dbo.sysmail_add_profileaccount_sp
@profile_name = @ProfileName,
@account_name = @AccountName,
@sequence_number = 1 ;


To send a email, I can use the following:

declare @recipients nvarchar(max)

select @recipients = isnull(@recipients,'') + useremail + ';'
from dbo.tbl_login where (status = "" and ReportDate < Date)


EXEC msdb.dbo.sp_send_dbmail
@recipients=@recipients,
@body='Add your Comments, blah blah...',
@subject ='Reminder Email - Please add your Comments',
@importance ='High',
@copy_recipients ='mycopy@mydomain.com',
@profile_name ='Send Reminder Emails'

Thanks!
0
 

Author Comment

by:sanjshah12
ID: 24930252
Just commenting again - I was hoping Tim would just clarify that the code is correct before I commit to the DB

Thanks
0
 
LVL 51

Assisted Solution

by:Mark Wills
Mark Wills earned 100 total points
ID: 25353177
Yes, it looks right - best way to test is to use literals in the sp_send_dbmail to test that you have the mail server set up properly (that can sometimes take a couple of attempts).

One thing that doesn't really look right (but maybe depending on your table) is if you are expecting multiple recipients...

Better to do :

declare @recipients nvarchar(max)
select @recipients = isnull(@recipients + ';','') + useremail from dbo.tbl_login where (status = "" and ReportDate < Date)

that way the semicolon is only being added where needed...
0
 
LVL 26

Assisted Solution

by:Zberteoc
Zberteoc earned 100 total points
ID: 25377332
The way I accomplished this in the past was by building the ASP page to send the emails as you said and then simply create a schedule in windows by Start > Programs > Accessories > System Tools > clicked on Sceduled Tasks and in the windows that opened clicked on Add Scheduled Task.

In the step by step wizard that opened I set it to use the Internet explorer with the link that pointed to the ASP page to sends the emails and scheduled it to be executed every day at the time I needed it. You can create a comand line for IT with the link is supposed to open in order to set up the task like:

C:\path\Iexplorer.exe "http://www.asppage.com/..."

Simple and efficient. I didn't have SQL server to use as I used an Access database for my application.

You need to use a computer that is on all the time though.
0

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to share SSIS Package? 6 37
Query Syntax 17 36
T-SQL: Do I need CLUSTERED here? 13 45
Sql server, import complete table, using vb.net 9 35
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.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…

810 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