为什么在SQL Server批量插入期间格式化文件会导致错误?

时间:2019-01-25 22:26:26

标签: sql-server bulkinsert

我正在尝试使用XML格式文件将CSV文件批量插入SQL Server 2017:

<?xml version="1.0"?>
<BCPFORMAT xmlns="https://schemas.microsoft.com/sqlserver/2004/bulkload/format" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <RECORD>
        <FIELD ID="Col_0" xsi:type="CharTerm" MAX_LENGTH="100" TERMINATOR=","/>
        <FIELD ID="Col_1" xsi:type="CharTerm" MAX_LENGTH="100" TERMINATOR=","/>
        <FIELD ID="Col_2" xsi:type="CharTerm" MAX_LENGTH="3" TERMINATOR=","/>
        <FIELD ID="Col_3" xsi:type="CharFixed" Length="19" TERMINATOR=","/>
        <FIELD ID="Col_4" xsi:type="CharFixed" Length="19" TERMINATOR=","/>
        <FIELD ID="Col_5" xsi:type="CharFixed" Length="53" TERMINATOR=","/>
        <FIELD ID="Col_6" xsi:type="CharFixed" Length="10" TERMINATOR=","/>
    </RECORD>
    <ROW>
        <COLUMN SOURCE="Col_0" NAME="NAME" xsi:type="SQLVARCHAR"/>
        <COLUMN SOURCE="Col_1" NAME="MATCHNAME" xsi:type="SQLVARCHAR"/>
        <COLUMN SOURCE="Col_2" NAME="CHROMOSOME" xsi:type="SQLVARCHAR"/>
        <COLUMN SOURCE="Col_3" NAME="START_LOCATION" xsi:type="SQLBIGINT"/>
        <COLUMN SOURCE="Col_4" NAME="END_LOCATION" xsi:type="SQLBIGINT"/>
        <COLUMN SOURCE="Col_5" NAME="CENTIMORGANS" xsi:type="SQLFLT4"/>
        <COLUMN SOURCE="Col_6" NAME="MATCHING_SNPS" xsi:type="SQLSMALLINT"/>
    </ROW>
</BCPFORMAT>

执行此操作时,出现以下错误消息:

  

执行期间出错    37000(4855)[Microsoft] [ODBC SQL Server驱动程序] [SQL Server]格式文件“ c:\ temp \ FormatFile.xml”中的第2行:意外元素“ BCPFORMAT”。

我不知为何无法识别BCPFORMAT元素。 XML已根据Altova XMYSpy正确格式化。

执行上传的代码是:

bulk insert FF_ChromBrwResults_20190124 from 'c:\temp\temp.csv'
 with (FIELDTERMINATOR = ',',FIRSTROW =1,ROWTERMINATOR = '\n'
,KEEPIDENTITY,CODEPAGE ='OEM',KEEPNULLS, FORMATFILE = 'c:\temp\FormatFile.xml' ) 

CSV有很多这种格式的行,以换行符结尾:

  

Name1,Name2,1,48745397,54260005,2.84,1000

我尝试从.NET代码和ODBC连接正常的AQT中执行此操作;两种方法产生相同的错误。

2 个答案:

答案 0 :(得分:3)

发生此错误是因为xmlns的URL包含正确的HTTPS为HTTP。

INCORRECT = <BCPFORMAT xmlns="https://schemas.microsoft.com/sqlserver/2004/bulkload/format" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

CORRECT = <BCPFORMAT xmlns="http://schemas.microsoft.com/sqlserver/2004/bulkload/format" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

一旦CharFixed的定界符不是逗号而是length属性,您的XML文件也需要进行一次修改,将所有CharFixed转换为CharTerm。

将所有类型的SQLVARCHAR都更改为SQLVARYCHAR

<?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="Col_0" xsi:type="CharTerm" MAX_LENGTH="100" TERMINATOR=","/>
        <FIELD ID="Col_1" xsi:type="CharTerm" MAX_LENGTH="100" TERMINATOR=","/>
        <FIELD ID="Col_2" xsi:type="CharTerm" MAX_LENGTH="3" TERMINATOR=","/>
        <FIELD ID="Col_3" xsi:type="CharTerm" TERMINATOR=","/>
        <FIELD ID="Col_4" xsi:type="CharTerm" TERMINATOR=","/>
        <FIELD ID="Col_5" xsi:type="CharTerm" TERMINATOR=","/>
        <FIELD ID="Col_6" xsi:type="CharTerm" TERMINATOR=","/>
    </RECORD>
    <ROW>
        <COLUMN SOURCE="Col_0" NAME="NAME" xsi:type="SQLVARYCHAR"/>
        <COLUMN SOURCE="Col_1" NAME="MATCHNAME" xsi:type="SQLVARYCHAR"/>
        <COLUMN SOURCE="Col_2" NAME="CHROMOSOME" xsi:type="SQLVARYCHAR"/>
        <COLUMN SOURCE="Col_3" NAME="START_LOCATION" xsi:type="SQLBIGINT"/>
        <COLUMN SOURCE="Col_4" NAME="END_LOCATION" xsi:type="SQLBIGINT"/>
        <COLUMN SOURCE="Col_5" NAME="CENTIMORGANS" xsi:type="SQLFLT4"/>
        <COLUMN SOURCE="Col_6" NAME="MATCHING_SNPS" xsi:type="SQLSMALLINT"/>
    </ROW>
</BCPFORMAT> 

答案 1 :(得分:2)

不是一个完整的答案,因为我发现格式文件是一个持久的谜,但是请尝试让BCP生成格式文件,例如:

bcp tempdb.dbo.FF_ChromBrwResults_20190124 format nul -c -x -t "," -f formatfile_gen.xml  -T -S localhost

该表的哪个

create table FF_ChromBrwResults_20190124
(
  NAME varchar(100),
  MATCHNAME varchar(100),
  CHROMOSOME varchar(30),
  START_LOCATION bigint,
  END_LOCATION bigint,
  CENTIMORGANS real,
  MATCHING_SNPS smallint
)

输出

<?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="100" COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
  <FIELD ID="2" xsi:type="CharTerm" TERMINATOR="," MAX_LENGTH="100" COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
  <FIELD ID="3" xsi:type="CharTerm" TERMINATOR="," MAX_LENGTH="30" COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
  <FIELD ID="4" xsi:type="CharTerm" TERMINATOR="," MAX_LENGTH="21"/>
  <FIELD ID="5" xsi:type="CharTerm" TERMINATOR="," MAX_LENGTH="21"/>
  <FIELD ID="6" xsi:type="CharTerm" TERMINATOR="," MAX_LENGTH="30"/>
  <FIELD ID="7" xsi:type="CharTerm" TERMINATOR="\r\n" MAX_LENGTH="7"/>
 </RECORD>
 <ROW>
  <COLUMN SOURCE="1" NAME="NAME" xsi:type="SQLVARYCHAR"/>
  <COLUMN SOURCE="2" NAME="MATCHNAME" xsi:type="SQLVARYCHAR"/>
  <COLUMN SOURCE="3" NAME="CHROMOSOME" xsi:type="SQLVARYCHAR"/>
  <COLUMN SOURCE="4" NAME="START_LOCATION" xsi:type="SQLBIGINT"/>
  <COLUMN SOURCE="5" NAME="END_LOCATION" xsi:type="SQLBIGINT"/>
  <COLUMN SOURCE="6" NAME="CENTIMORGANS" xsi:type="SQLFLT4"/>
  <COLUMN SOURCE="7" NAME="MATCHING_SNPS" xsi:type="SQLSMALLINT"/>
 </ROW>
</BCPFORMAT>