Solved

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

Posted on 2013-05-16
6
419 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
[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
  • 4
  • 2
6 Comments
 
LVL 25

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 25

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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.
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.

626 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