我正在准备格式文件,以便根据This article
将文本限定文件导入sql-server导入数据样本:
"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
结果是:
批量加载失败。第1行第6列的数据文件中的列太长。验证是否正确指定了字段终止符和行终止符。
我很确定该文件包含\ r \ n(使用十六进制编辑器检查显示0x0d,0x0a),但忽略文本限定符和格式文件我只能手动导入它
Bulk insert <table_name> from '\\path\to\file' with (fieldterminator='|', rowterminator='\n')
答案 0 :(得分:1)
我试着戳你的格式文件,但它刚刚确认我对该版本的格式文件没有任何好处。
切换到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
);
结果:
输入文件,在记事本++中显示行终止符: