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 2008 Forums
 Transact-SQL (2008)
 export query results into Excel Report

Author  Topic 

Ratz03
Starting Member

27 Posts

Posted - 2015-01-14 : 09:15:43
Hi Experts,

Could you please help me in writing a piece of code for stored procedure to export query results to an excel sheet.

Thankyou

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2015-01-14 : 09:44:40
Easier to use SSIS. All you need is built-in
Go to Top of Page

jackv
Master Smack Fu Yak Hacker

2179 Posts

Posted - 2015-01-19 : 01:40:14
@Ratz03 - another method to think about depending on your requirements , is to use Powershell.In the example,a connection is made to a SQL server Instance , a recordset is returned , placed into a DataSet and then written to a new Excel worksheet
http://www.sqlserver-dba.com/2013/05/sql-server-export-to-excel-with-powershell.html

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

Ratz03
Starting Member

27 Posts

Posted - 2015-01-19 : 11:32:12
Thankyou jackv. Powershell looked complicated as i'm a newbie with SQL.

@gbritton: I used SSIS package and was able to export my query results to a excel. However, what the query returns is a report which is monthly. Is there a way using SSIS package where file is created eachtime with the date in the file name at the same location.

Also, it is feasible to have 2 queries returning data on different tabs of xcel. This is the requirement for my report.

Many Thanks.
Go to Top of Page

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2015-01-19 : 12:02:44
" Is there a way using SSIS package where file is created eachtime with the date in the file name at the same location."

Yes -- on the Excel file connector propertieds, you can set the filename to be an expression that you build up the way you want.

" it is feasible to have 2 queries returning data on different tabs of xcel. This is the requirement for my report."

Yup -- I do it all the time. You use two data flow tasks.
Go to Top of Page

jackv
Master Smack Fu Yak Hacker

2179 Posts

Posted - 2015-01-20 : 01:50:54
@Ratz03 - getting in powershell is very easy. To start focus on these three cmdlets. http://www.sqlserver-dba.com/2013/06/expand-your-powershell-mind-three-key-cmdlets-1.html
One of the aims of powershell is to simplify administration

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

- Advertisement -