Solved

Advice: Open a page or run a query every day

Posted on 2009-07-11
9
218 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
 
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

708 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now