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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 How to save SQL output as EXCEL file?

Author  Topic 

DBADave
Constraint Violating Yak Guru

366 Posts

Posted - 2008-05-05 : 02:12:44
I would like to create a SQL job that runs a basic query monthly and saves the results as a .xls file and emails the results to a user. I've been unsuccessful with sp_send_dbmail in that the output file is always a single column. Any suggestions?

Thanks, Dave

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-05-05 : 02:17:43
You can use bcp query out option to export data to excel file. Refer link to find a stored procedure that does this

http://weblogs.sqlteam.com/mladenp/archive/2006/07/25/10771.aspx

You just need your job to call this procedure
Go to Top of Page

rmiao
Master Smack Fu Yak Hacker

7266 Posts

Posted - 2008-05-05 : 22:52:15
Or with sqlcmd.
Go to Top of Page

DBADave
Constraint Violating Yak Guru

366 Posts

Posted - 2008-05-06 : 14:59:01
I tried SQLCMD, but was unable to get the results to appear in separate columns on the spreadsheet.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-05-06 : 15:07:50
If you want to use sqlcmd, then you'll need to concatenate all of your outputted columns together and separate them with a comma. So you are CSV'ing them manually.

I'd just use bcp though. That's what we've got setup in production for a similar problem.

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

Database maintenance routines:
http://weblogs.sqlteam.com/tarad/archive/2004/07/02/1705.aspx
Go to Top of Page

DBADave
Constraint Violating Yak Guru

366 Posts

Posted - 2008-05-06 : 15:13:45
Thanks Tara
Go to Top of Page
   

- Advertisement -