Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Advice: Open a page or run a query every day

Posted on 2009-07-11
9
Medium Priority
?
228 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 300 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 600 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
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
LVL 60

Assisted Solution

by:chapmandew
chapmandew earned 600 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 300 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 300 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

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

636 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