Solved

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

Posted on 2013-05-16
6
410 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
First Max value 3 33
job schedule 8 21
.NET Enums [Flags] & Bitwise Design Question 6 30
Need to find substring in SQL 5 21
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.
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…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
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.

821 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