2013年10月20日 星期日

OPENROWSET導致SQL Server服務關閉

Windows 8 + SQL 2012 (11.0.3128)

症頭是使用OPENROWSET導致SQL Server服務關閉。
 select * 
  from OPENROWSET('Microsoft.ACE.OLEDB.12.0',
  'Excel 12.0;HDR=YES;IMEX=1;Database=c:\temp\ImportUsers.xlsx',
  'select * from [Users$]')  


查看事件檢視器,就這麼簡短的一行字
SQL Server (MSSQLSERVER) 服務意外地終止。已經發生 1 次。

另外還有項看不懂的Application Error
失敗的應用程式名稱: sqlservr.exe,版本: 2011.110.3128.0,時間戳記: 0x50deadad
失敗的模組名稱: ntdll.dll,版本: 6.2.9200.16579,時間戳記: 0x51637f77
例外狀況代碼: 0xc0000374
錯誤位移: 0x00000000000ebd59
失敗的處理程序識別碼: 0x61c
失敗的應用程式開始時間: 0x01cecd9d9186c411
失敗的應用程式路徑: C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\Binn\sqlservr.exe
失敗的模組路徑: C:\Windows\SYSTEM32\ntdll.dll
報告識別碼: af519ed8-3991-11e3-be9a-0c6076872559
失敗的套件完整名稱:
失敗的套件相關應用程式識別碼:




 同一個EXCEL檔轉成ImportUsers.xls使用SQL 2005讀取,卻沒有問題
 select * 
  from OPENROWSET('Microsoft.Jet.OLEDB.4.0',
  'Excel 8.0;HDR=YES;IMEX=1;Database=c:\temp\ImportOrgnUsers.xls',
  'select * from [Users$]')   


 找到這篇文章,解藥如下

EXEC master.dbo.sp_MSset_oledb_prop 
N'Microsoft.ACE.OLEDB.12.0', N'AllowInProcess', 1  

EXEC master.dbo.sp_MSset_oledb_prop

 N'Microsoft.ACE.OLEDB.12.0', N'DynamicParameters',1

試了一下,執行 AllowInProcess即可解決。

順便查了一下何謂 AllowInProcess  Anish Shenoy說到

AllowInProcess=0 (default)
Running the process outside the SQL Server Process:

Allowing the provider to run outside the SQL Server Process helps to protect the SQL Server process from errors in the provider, as when the provider runs outside the SQL Server process Updates or inserts involving column data types like (varchar(max), nvarchar(max), varbinary(max), text, ntext, or image) are not allowed.


AllowInProcess=1
Running the process inside the SQL Server Process:
By setting this option in the SQL Server providers, we will be able to handle the data types like (varchar(max), nvarchar(max), varbinary(max), text, ntext, or image) including long columns, image and text data.

In case of excel data we can see that when we import an excel data into the database tables without having predefined datatypes, by default the datatypes of some of the columns may be assigned to nvarchar(max) or ntext etc, so if we do net set the Allow Inprocess it will throw error.
 
這讓我順便抱怨一下,在SQL2012使用 撰寫SQL 指令 Import /Export資料時,在資料表對映無法解析到正確的資料類型 ,datatype 202 是什麼東西啊。



剛好也了解到原來這算是是一個BUG嗎?

nvarcahr 和varchar在SP1取消支援,改成了[DT_WSTR]和[DT_STR],把SSIS的定義套用在SSMS,真是很奇怪。

沒有留言:

張貼留言

publish error allowDefinition='MachineToApplication'

一個老舊的aspx web form專案,調了一些功能建置成功,但進行部署時顯示以下錯誤。 在應用程式層級之外使用註冊為 allowDefinition='MachineToApplication' 的區段發生錯誤。錯誤的原因可能是虛擬目錄尚未在 IIS 中設定為...