批量插入不识别行终止符

Bulk insert not recognizing row terminators

我正在准备格式文件以将文本限定文件导入到 sql-基于 This article

的服务器

导入数据样本:

"1000000"|"1100000"|"2017-02-26 00:00:00"|"CAT1"|"Item from CAT1"
"1000001"|"1100000"|"2017-02-26 00:00:01"|"CAT2"|"Item from CAT2"
"1000002"|"1100001"|"2017-02-26 00:01:02"|"CAT2"|"Item from CAT2"
"1000003"|"1100002"|"2017-02-26 01:02:03"|"CAT3"|"Item from CAT3"

我的格式文件:

13.0
6
1       SQLCHAR             0       0       "\""        0     FIRST_QUOTE                     SQL_Latin1_General_CP1_CI_AS
2       SQLINT              0       4       "\"|\""     1     transaction_id                  ""
3       SQLINT              1       4       "\"|\""     2     user_id                         ""
4       SQLDATETIME         0       8       "\"|\""     3     create_date                     ""
5       SQLCHAR             2       10      "\"|\""     4     category                        SQL_Latin1_General_CP1_CI_AS
6       SQLCHAR             2       50      "\"\r\n"    5     item                            SQL_Latin1_General_CP1_CI_AS

这导致:

The bulk load failed. The column is too long in the data file for row 1, column 6. Verify that the field terminator and row terminator are specified correctly.

我相当确定该文件包含 \r\n(使用十六进制编辑器检查显示 0x0d、0x0a),尽管忽略文本限定符和格式文件我只能使用

手动导入它
Bulk insert <table_name> from '\path\to\file' with (fieldterminator='|', rowterminator='\n')

我试过戳你的格式文件,但它只是证实我对那个版本的格式文件没有任何好处。

切换到 xml 格式文件很容易。

cat.xml:

<?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='"'     COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
<FIELD ID="2" xsi:type="CharTerm" TERMINATOR='"|"'   COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
<FIELD ID="3" xsi:type="CharTerm" TERMINATOR='"|"'   COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
<FIELD ID="4" xsi:type="CharTerm" TERMINATOR='"|"'   COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
<FIELD ID="5" xsi:type="CharTerm" TERMINATOR='"|"'   COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
<FIELD ID="6" xsi:type="CharTerm" TERMINATOR='"\r\n' COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
</RECORD>
<ROW>
<COLUMN SOURCE="2" NAME="transaction_id" xsi:type="SQLVARYCHAR"/>
<COLUMN SOURCE="3" NAME="user_id" xsi:type="SQLVARYCHAR"/>
<COLUMN SOURCE="4" NAME="create_date" xsi:type="SQLVARYCHAR"/>
<COLUMN SOURCE="5" NAME="category" xsi:type="SQLVARYCHAR"/>
<COLUMN SOURCE="6" NAME="item" xsi:type="SQLVARYCHAR"/>
</ROW>
</BCPFORMAT>

sql:

bulk insert dbo.cat
   from 'c:\cat.txt'
   with (
      formatfile = 'c:\cat.xml'
    , firstrow   = 1
  );

结果:

输入文件,在记事本++中显示行终止符: