Click here to Skip to main content
15,892,537 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I want save my sql query result as excel file.
I use from bellow code, but it doesn't work correctly:

insert into OPENROWSET('Microsoft.Jet.OLEDB.4.0', 
'Excel 8.0;Database=D:\testing.xls;', 
'SELECT * FROM [Sheet1$]') 
select * from DataDictionary


My Error:
Msg 7302, Level 16, State 1, Line 1
Cannot create an instance of OLE DB provider "Microsoft.Jet.OLEDB.4.0" for linked server "(null)".

I use SQL Server 2012 and Excel 2007.

Please help me, How I can save result of my query as excel file?
Thanks a lot
Posted
Comments
Member 10463904 21-Feb-14 2:12am    
Pls help me how to export sql query result in excel... Tell me full coding explanation.. im new to sql
Reza Alipour Fard 21-Feb-14 14:13pm    
Please see my solution, I used from bellow query. for example:
DECLARE @cmd VARCHAR(255)
SET @cmd = 'bcp "Select * From TestTable" queryout "C:\TestFile.xlsx" -U sa -P 123 -c'
Exec xp_cmdshell @cmd

Have a look here:
Query Excel file source through Linked Server[^]
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=49926[^]
http://www.mssqltips.com/sqlservertip/1202/export-data-from-sql-server-to-excel/[^]

Example:
SQL
insert into OPENROWSET('Microsoft.ACE.OLEDB.12.0', 
'Excel 12.0;Database=D:\testing.xlsx;', 
'SELECT * FROM [Sheet1$]') 
select * from DataDictionary


Another way is to use bcp and xp_cmdshell tools (to *.csv files):
Exporting Data Programatically with bcp and xp_cmdshell[^]
 
Share this answer
 
v3
Comments
Reza Alipour Fard 8-Jul-13 6:05am    
I use from all of the introduced ways, but all of them is not work correctly.
Reza Alipour Fard 8-Jul-13 6:06am    
Please attend to my problem after that give me a clear sample.
Maciej Los 8-Jul-13 6:10am    
See my answer now ;)
Reza Alipour Fard 8-Jul-13 6:33am    
I have erro:
Msg 7302, Level 16, State 1, Line 1
Cannot create an instance of OLE DB provider "Microsoft.ACE.OLEDB.12.0" for linked server "(null)".
Hi
I solved my problem.

SQL
DECLARE @cmd VARCHAR(255)
SET @cmd = 'bcp "My Select Query Here" queryout "My Excel File Path Here" -U sqluserHere -P sqlpasswordHere -c'
Exec xp_cmdshell @cmd
 
Share this answer
 
Comments
Meysam Toluie 26-Apr-14 4:43am    
I executed this code:
DECLARE @cmd VARCHAR(255)
SET @cmd = 'bcp "Select Distinct Loan.ManagedFundAccNo1 From Account.Loan.Loan" queryout "D:\test.xls" -U sa -P 123 -c'
Exec xp_cmdshell @cmd

Then the following errors came as result.

output
SQLState = 08001, NativeError = 2
Error = [Microsoft][SQL Server Native Client 10.0]Named Pipes Provider: Could not open a connection to SQL Server [2].
SQLState = 08001, NativeError = 2
Error = [Microsoft][SQL Server Native Client 10.0]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is config
ured to allow remote connections. For more information see SQL Server Books Online.
SQLState = S1T00, NativeError = 0
Error = [Microsoft][SQL Server Native Client 10.0]Login timeout expired
NULL
Reza Alipour Fard 2-Jun-14 10:17am    
Please check Xp_Cmdshell status in your SQL Server.
Member 11027043 22-Oct-15 23:30pm    
Hi, this code works perfectly :)

DECLARE @cmd VARCHAR(255)
SET @cmd = 'bcp "My Select Query Here" queryout "My Excel File Path Here" -U sqluserHere -P sqlpasswordHere -c'
Exec xp_cmdshell @cmd

But I need the column name.
How can I get the column name at the first row in my excel?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900