2016-02-29 94 views
2

我將查詢結果保存到SQL Server 2008中的表my_table中,然後希望將表中的數據插入到Excel 2007文件中。下面是查詢我using-將sql查詢結果導出爲ex​​cel文件

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分佈式查詢似乎在我的系統上啓用。 enter image description here

+2

確保您已啓用[Ad Hoc Distributed Queries](http://stackoverflow.com/questions/14544221/how-to-enable-ad-hoc-distributed-queries)。注意這是一個服務器範圍的設置。如果你的SQL Server是64位的,也可以看看[this](http://dba.stackexchange.com/questions/68050/distributed-queries-are-configured-to-run-in-single-threaded-apartment模式)問題。 [這裏](http://dba.stackexchange.com/questions/54675/openrowset-how-to-configure-ole-db-provider-to-be-used-for-for-distributed-que)是一個問題,其中有人正在使用較新的供應商。 –

+0

在打開'testing.xls'並查詢表格時是否有益處?拉而不是推? –

+0

@BaconBits Ad Hoc分佈式查詢選項似乎在我的系統上啓用。我使用顯示配置的快照更新了問題。 – Karver01

回答

0

您需要查詢之前,運行此:

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 

後您的查詢中設置這個值設置爲0,如果你所需要的。 如果運行64位服務器,則需要使用Microsoft.ACE.OLEDB.12.0