在旅途中将sql查询结果导出到excel文件

时间:2016-02-29 18:10:48

标签: sql-server excel

我将查询结果保存到SQL Server 2008中的表my_table中,然后将表中的数据插入到excel 2007文件中。以下是我正在使用的查询 -

INSERT INTO OPENROWSET('Microsoft.Jet.OLEDB.4.0', 
'Excel 8.0;Database=C:\testing.xls;', 
'SELECT Name, Date FROM [Sheet1$]') 
SELECT a, b FROM my_table
GO 

显示以下错误

Msg 7308, Level 16, State 1, Line 1
OLE DB provider 'Microsoft.Jet.OLEDB.4.0' cannot be used for distributed queries because the provider is configured to run in single-threaded apartment mode.

我怎样才能解决这个问题?附: Ad Hoc Distributed Queries似乎在我的系统上启用。 enter image description here

1 个答案:

答案 0 :(得分:0)

您需要在查询之前运行它:

mm WITH_DEXPREOPT=false -B

如果需要,在查询后将此值设置为0。 如果运行64位服务器,则需要使用EXEC master.dbo.sp_MSset_oledb_prop N'Microsoft.Jet.OLEDB.4.0', N'AllowInProcess', 1 GO EXEC master.dbo.sp_MSset_oledb_prop N'Microsoft.Jet.OLEDB.4.0', N'DynamicParameters', 1 GO