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
 Send Results of SQL query through Email

Author  Topic 

swims01
Yak Posting Veteran

59 Posts

Posted - 2009-05-05 : 16:22:35
Hey all. I'm searching through the site right now but a lot of the results are more complicated than what I'm trying to do.

I've created my SQL query (Saved as myscript.sql)
I've set up my mail profile within SQL Server 2005 and tested it with success.

How can I set my query to run at a specific time every night and send the results through email?

Thanks and I'll continue to search in the mean time.

edit* I found the following from another site. It has the query within the code but I'd like to reference my file instead of putting my entire query in there...

EXEC msdb.dbo.sp_send_dbmail
@recipients=N'chapman.tim@gmail.com',@body='Message Body',
@subject ='Message Subject',@profile_name ='DatabaseMailProfile',@query ='SELECT Product FROM sb2..SalesHistory GROUP BY Product HAVING COUNT(*) > 3',
@attach_query_result_as_file = 1,@query_attachment_filename ='Results.txt'

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-05-05 : 17:16:02
Just add an attachment. Look at sp_send_dbmail in SQL Server Books Online for more information.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

swims01
Yak Posting Veteran

59 Posts

Posted - 2009-05-05 : 17:37:59
I'd actually prefer to send the results of the query in the body of the email. I just need to see if it's possible to reference the SQL script instead of inserting the actual code.

Ex.
EXEC msdb.dbo.sp_send_dbmail
@recipients=N'user@domain.com',@body='Message Body',
@subject ='Message Subject',@profile_name ='DatabaseMailProfile',@query = "C:\myscript.sql",
@attach_query_result_as_file = 0



It's the [ @query = "C:\myscript.sql" ] statement that I'm interested in. If it can reference a .SQL file then I'm in the clear. But if I have to manually insert my query into that then it's not the end of the world, but I'll have to remember to update in the SQL Job after I make edits to the actual file.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-05-05 : 17:40:05
You can't reference the query inside the sql file.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

swims01
Yak Posting Veteran

59 Posts

Posted - 2009-05-05 : 17:50:46
Ok, thanks.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-05-05 : 18:03:30
I should state that you can't directly do it, but you could do it if you wrote code. You could get the contents of the file into a table via xp_cmdshell and type command, and then get the table rows into a variable. Once it's in a variable, that's when you can use it with Database Mail.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page
   

- Advertisement -