Solved

Advice: Open a page or run a query every day

Posted on 2009-07-11
9
223 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
[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
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 27

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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

726 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