Solved

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

Posted on 2013-05-16
6
415 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
Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

 
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

Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
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.
Viewers will learn how the fundamental information of how to create a table.

728 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