Solved

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

Posted on 2013-05-16
6
401 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
Comment Utility
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
Comment Utility
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
Comment Utility
To clarify, I need to email a csv or xsl file of the sql server agent query results.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 24

Accepted Solution

by:
chaau earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

762 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

8 Experts available now in Live!

Get 1:1 Help Now