让我提供一些细节,然后我希望这个问题会更清楚一些。数据由|分隔。原因是某些字段必须保留逗号并使其成为csv会导致它过于复杂。
示例数据
12007|0|10|70|0|2017|1|1|Some County, Nevada|Total Covered|10 Total,all industries
12007|0|10|70|0|2017|2|1|Some County, Nevada|Total Covered|10 Total,all industries
创建表脚本
create table test (
area_fips varchar(5),
own_code varchar(1),
industry_code varchar(6),
agglvl_code varchar(6),
size_code varchar(2),
year varchar(50),
qtr varchar(50),
Disclosure_code varchar(50),
area_title varchar(50),
own_title varchar(50),
industry_title varchar(254)
);
批量插入命令
bulk insert test
From 'Q:\folder\test1.txt'
WITH (firstrow=2,FORMATFILE='Q:\folder\xml1.xml',
ERRORFILE='Q:\folder\error4.txt'
);
XML格式文件
<?xml version="1.0"?>
<BCPFORMAT xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://schemas.microsoft.com/sqlserver/2004/bulkload/format">
-<RECORD>
<FIELD MAX_LENGTH="5" TERMINATOR="|" xsi:type="CharTerm" ID="1"/>
<FIELD MAX_LENGTH="1" TERMINATOR="|" xsi:type="CharTerm" ID="2"/>
<FIELD MAX_LENGTH="6" TERMINATOR="|" xsi:type="CharTerm" ID="3"/>
<FIELD MAX_LENGTH="6" TERMINATOR="|" xsi:type="CharTerm" ID="4"/>
<FIELD MAX_LENGTH="2" TERMINATOR="|" xsi:type="CharTerm" ID="5"/>
<FIELD MAX_LENGTH="50" TERMINATOR="|" xsi:type="CharTerm" ID="6"/>
<FIELD MAX_LENGTH="50" TERMINATOR="|" xsi:type="CharTerm" ID="7"/>
<FIELD MAX_LENGTH="50" TERMINATOR="|" xsi:type="CharTerm" ID="8"/>
<FIELD MAX_LENGTH="50" TERMINATOR="|" xsi:type="CharTerm" ID="9"/>
<FIELD MAX_LENGTH="50" TERMINATOR="|" xsi:type="CharTerm" ID="10"/>
<FIELD MAX_LENGTH="200" TERMINATOR="\r\n" xsi:type="CharTerm" ID="11"/>
</RECORD>
-<ROW>
<COLUMN xsi:type="SQLNVARCHAR" NAME="areafips" SOURCE="1"/>
<COLUMN xsi:type="SQLNVARCHAR" NAME="owncode" SOURCE="2"/>
<COLUMN xsi:type="SQLNVARCHAR" NAME="indcode" SOURCE="3"/>
<COLUMN xsi:type="SQLNVARCHAR" NAME="agglvlcode" SOURCE="4"/>
<COLUMN xsi:type="SQLNVARCHAR" NAME="sizecode" SOURCE="5"/>
<COLUMN xsi:type="SQLNVARCHAR" NAME="year" SOURCE="6"/>
<COLUMN xsi:type="SQLNVARCHAR" NAME="qtr" SOURCE="7"/>
<COLUMN xsi:type="SQLNVARCHAR" NAME="disclosurecode" SOURCE="8"/>
<COLUMN xsi:type="SQLNVARCHAR" NAME="areatitle" SOURCE="9"/>
<COLUMN xsi:type="SQLNVARCHAR" NAME="owntitle" SOURCE="10"/>
<COLUMN xsi:type="SQLNVARCHAR" NAME="industrytitle" SOURCE="11"/>
</ROW>
</BCPFORMAT>
错误消息
Msg 4832, Level 16, State 1, Line 15
Bulk load: An unexpected end of file was encountered in the data file.
错误文件
Row 2 File Offset 161 ErrorFile Offset 0 - HRESULT 0x80004005
12007|0|10|70|0|2017|2||Some County, Nevada|Total
Covered12007|0|10|70|0|2017|2||Some County, Nevada|Total Covered
所以它似乎无法识别第10和第11个字段之间的I。它之前认识到它们,但是在最后一个它上面,它失败了。
答案 0 :(得分:0)
答案并不像问题那样复杂。无论如何,花了6个星期才弄清楚了。
BULK INSERT dbo.test
FROM 'Q:\folder\test1.txt'
WITH
(
FIELDTERMINATOR = '|'
);
我的印象是,如果它是123.45,我必须在格式文件中指定为数字(5.2)。好吧,显然不是。没有格式文件,并省略了行终止符,导入正常。