SQL:OPENROWSET无法正确返回数据

时间:2015-04-30 19:22:42

标签: sql tsql powershell sql-server-2012 openrowset

我正在尝试使用OPENROWSET查询csv文件,该文件在90%的时间内运行良好但由于某些原因,某些.csv文件返回此错误:

Msg 4863, Level 16, State 1, Line 1
Bulk load data conversion error (truncation) for row 1, column 5 (Status Description).

或此错误:

Msg 4832, Level 16, State 1, Line 1
Bulk load: An unexpected end of file was encountered in the data file.

我的查询如下所示:

select * from OPENROWSET(BULK 'E:\File.csv', FORMATFILE= 'E:\schema.xml') AS a

我的格式文件如下所示:

<?xml version="1.0"?>
<BCPFORMAT xmlns="http://schemas.microsoft.com/sqlserver/2004/bulkload/format" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<RECORD>
 <FIELD ID="1" xsi:type="CharTerm" TERMINATOR="," MAX_LENGTH="1000" COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
 <FIELD ID="2" xsi:type="CharTerm" TERMINATOR="," MAX_LENGTH="1000" COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
 <FIELD ID="3" xsi:type="CharTerm" TERMINATOR="," MAX_LENGTH="1000" COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
 <FIELD ID="4" xsi:type="CharTerm" TERMINATOR="," MAX_LENGTH="1000" COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
 <FIELD ID="5" xsi:type="CharTerm" TERMINATOR="\r\0\n\0" MAX_LENGTH="1000" COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
</RECORD>
<ROW>
 <COLUMN SOURCE="1" NAME="Column1" xsi:type="SQLNVARCHAR"/>
 <COLUMN SOURCE="2" NAME="Column2" xsi:type="SQLNVARCHAR"/>
 <COLUMN SOURCE="3" NAME="Column3" xsi:type="SQLNVARCHAR"/>
 <COLUMN SOURCE="4" NAME="Column4" xsi:type="SQLNVARCHAR"/>
 <COLUMN SOURCE="5" NAME="Column5" xsi:type="SQLCHAR"/>
 </ROW>
</BCPFORMAT>

我发现如果我将.csv的内容复制到一个全新的文件并保存,然后再次运行,查询将成功完成。但这并不理想,因此在调整格式文件并运行相同的查询之后,我现在得到了这个结果:

Column 1 Column2 Column3 Column4
ÿþD             
m               
m               
m               
m               
m               

当我的原始数据如下所示:

 Column 1 Column2   Column3 Column4
 Abc      elephant  Yes     Job has finished.               
 def      tiger     Yes     Job has finished.
 xyz      monkey    Yes     Job has finished.   
 ghi      dog       Yes     Job has finished.       

现在看来查询正在完成但是正在返回垃圾数据。

有谁知道如何解决这个问题,以便我可以返回准确的结果?

0 个答案:

没有答案