How to preset which datatype must be fetched from excel to sql

蹲街弑〆低调 提交于 2019-12-12 01:58:02

问题


I use 64-bit driver. In order to obtain data from excel I use the following select:

SELECT * FROM OPENDATASOURCE('Microsoft.ACE.OLEDB.12.0',
  'Data Source=C:\path\to\file.xlsx;Extended Properties=Excel 12.0')...[db1$]

However when there are too much first (say 5000) rows containg NULLs it sets wrong datatype and convert (corrupt) the data to the DB.

Is it possible to preset or preverify more than 5000 rows?

Note that, 5000 isn't accurate number.

Weird, following code returns corrupted data as well:

SELECT * FROM OPENDATASOURCE('Microsoft.ACE.OLEDB.12.0',
  'Data Source=C:\path\to\file.xlsx;Extended Properties="Excel 12.0 Xml;HDR=YES;IMEX=1"')...[db1$]

That said IMEX=1 doesn't work...

Update: TypeGuessRows=0 in reg fixes it!


回答1:


Does the IMEX=1 setting mentioned here help?

http://www.connectionstrings.com/excel/

If not that, does this help?

http://jingyangli.wordpress.com/2009/02/13/imex1-revisit-and-typeguessrows-setting-change-to-0-watch-for-performance/

32 bit:

Hkey_Local_Machine/Software/Microsoft/Jet/4.0/Engines/Excel/TypeGuessRows Hkey_Local_Machine/Software/Microsoft/Jet/4.0/Engines/Excel/ImportMixedTypes

64 bit: HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Jet\4.0\Engines\Excel\TypeGuessRows HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Jet\4.0\Engines\Excel\ImportMixedTypes

Change: TypeGuessRows: 0 —-Check all column values before choosing the appropriate data type.ImportMixedTypes: Text —-import mixed-type columns as text.




回答2:


Possible workaround:

Add 1st data row to the excel with values of the correct type and then just exclude it from your results.



来源:https://stackoverflow.com/questions/24933307/how-to-preset-which-datatype-must-be-fetched-from-excel-to-sql

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!