Solved

How to create a daily or weekly email query in MS SQL 2008 Standard

Posted on 2013-05-16
6
405 Views
Last Modified: 2013-05-27
Hi, we have MS SQL 2008 Standard. I have a table in a database that I need to email only the new records than popular end of each day. I also need to email all new records in the table end of each Friday.

 I am not very familiar with SSMS and SQL queries (some experience with MS Access) and am looking for some basic information on where to start.
0
Comment
Question by:Mark B
  • 4
  • 2
6 Comments
 
LVL 24

Expert Comment

by:chaau
ID: 39173809
You should look at SQL Server Agent. There, you can create jobs, that will run by the scheduler. The job results can be emailed. It is a built-in feature of SQL Server, and is very easy configurable via SQL Server Management console UI.

Please provide more specific details, and we will be able to get you started
0
 

Author Comment

by:Mark B
ID: 39180076
Ok, I've setup a database mail profile in SQL Server 2008 via SQL Server Management console and sent a test email ok. I've checked SQL server Agent and see that you can create jobs and step and schedule tasks.

What I need to do simply first, is select all records of a table and email the results. From, here I guess I can work on refining the query results.

Are there any tutorials that can walk me through setting this up?
0
 

Author Comment

by:Mark B
ID: 39180086
To clarify, I need to email a csv or xsl file of the sql server agent query results.
0
[Webinar] Disaster Recovery and Cloud Management

Learn from Unigma and CloudBerry industry veterans which providers are best for certain use cases and how to lower cloud costs, how to grow your Managed Services practice in IaaS clouds, and how to utilize public cloud for Disaster Recovery

 
LVL 24

Accepted Solution

by:
chaau earned 500 total points
ID: 39180124
That's great. That was one of the complex parts. Now, you need to start the Agent (SQL Server may ask you to create accounts and some other stuff, just follow the instructions in MSDN).

Now, create a job, add a new step.  Select "Transact-SQL script" as a type. Add this SQL command:

EXEC msdb.dbo.sp_send_dbmail
@profile_name= 'Exchange Server',
@recipients= 'alphagraphics297@yourcomapny.com',
@subject='Log Warning',
@query='SELECT [ID],[Category],[JobID],[DBDateTime]  FROM [yourDB].[dbo].[JobLog]'

Open in new window

You'll need to enter proper @profile_name (the easiest to find out the profile name is when you use "Send Test Email..." option. The profile will be there.

You do not need to "Notifications" option of the job, as emails will be sent by msdb.dbo.sp_send_dbmail stored procedure. Now, jus right click on the job and select "Start Job at Step..." to test. If it works, setup the scheduler
0
 

Author Comment

by:Mark B
ID: 39199744
Ok, I've been able to setup a sql server agent job to email a query result, but the query result shows in the email body. I need to attach the query result as a csv or xls file. Any input on this would be great. Thanks for your help.
0
 

Author Comment

by:Mark B
ID: 39199824
I believe I've got the csv export to work based on this post:

http://sqlworkday.blogspot.com/2010/12/how-to-send-csv-or-xls-file-as.html?m=1

Thanks again
0

Featured Post

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
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
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

896 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

13 Experts available now in Live!

Get 1:1 Help Now