?
Solved

Advice: Open a page or run a query every day

Posted on 2009-07-11
9
Medium Priority
?
224 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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

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

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…
I have a large data set and a SSIS package. How can I load this file in multi threading?
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

762 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