Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Automating SQL queries and emailing the results

Author  Topic 

Topaz
Posting Yak Master

199 Posts

Posted - 2011-01-14 : 10:22:58
Each week I have to run a repetitive SQL query to find out how well some of my data is performing and then email it to our sales department for more analysis. It would be awesome if I could automate this process...

I've been told that to automate the whole process I could use a SQL Server Agent Job. Being a novice I'm not sure where to start and when I right click SQL Server Agent it asks me to 'start' as it has been disabled. (if I do this it's not going to delete or mess up any of my server settings?)

Please advise



JT

dinakar
Master Smack Fu Yak Hacker

2507 Posts

Posted - 2011-01-15 : 04:13:20
You need to create a job and put your SQL query as a step in the job. The SQL Agent is a tool to manage your jobs. heres something to get you started - http://msdn.microsoft.com/en-us/library/ms187910.aspx


Dinakar Nethi
************************
Life is short. Enjoy it.
************************
http://weblogs.sqlteam.com/dinakar/
Go to Top of Page

jackv
Master Smack Fu Yak Hacker

2179 Posts

Posted - 2011-01-15 : 06:11:58
Consider configuring Database Mail on SQL Server 2005

Jack Vamvas
--------------------
http://www.sqlserver-dba.com
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-01-16 : 05:29:29
you need to first start the sql server agent. then sql server qgent service starts running. then you can go to management studio connect to your database in object explorer and start creating jobs. see link below for step by step procedure

http://databases.about.com/od/sqlserver/ss/sql_server_agent.htm

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Topaz
Posting Yak Master

199 Posts

Posted - 2011-01-20 : 06:59:29
This may sound like a complete 'newbie' question but is there any harm that I can do to my database if I turn 'start' SQL server agent?

JT
Go to Top of Page

Topaz
Posting Yak Master

199 Posts

Posted - 2011-01-20 : 07:30:02
Ok, so I've started the SQL Server Agent. Setup a job that allows me to run a few steps and then email me when the job has been completed.

What I would now like to know is can I get my SQL server to email me the total number of records that is produced as a result of completing the steps?

JT
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-01-24 : 11:11:43
you can by capturing the rows affected in respective steps by using @@ROWCOUNT variable

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

jackv
Master Smack Fu Yak Hacker

2179 Posts

Posted - 2011-01-31 : 12:33:55
This is a simple example ,and works on the assumption you are on at least sql server 2005 and have Database mail , properly configured:
EXEC msdb.dbo.sp_send_dbmail
@recipients=N'JackVamvas@sqlserver-dba.com',
@body= 'Some text for body',
@subject = 'Some text for subject',
@profile_name = 'MyProfile1',
@query = ‘select name from sys.sysdatabases’,
@attach_query_result_as_file = 1;


In what object are is your resultset being currently held?




Jack Vamvas
--------------------
http://www.sqlserver-dba.com
Go to Top of Page
   

- Advertisement -