我有很多平面(文本)文件要每天导入到 SQLSERVER 表格中。 目前, 在我制定程序时, 我只想导入一个文件。 当然我可以为此写入 C# 代码, 但我觉得这不是正确的方式, 我想用xml 格式文件来大量插入。
我的第一个样本文件看起来像这样( 样本. dat) :
Q RR201110010000000002000000000000232000
N X4201110010000000001500000000000160000
注:本文件中的一十六式倾斜显示,每条线的终止都有一个确切的新行字符,没有多少是新行字符。
我的 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="CharFixed" LENGTH="6"/>
<FIELD ID="2" xsi:type="CharFixed" LENGTH="2"/>
<FIELD ID="3" xsi:type="CharFixed" LENGTH="8"/>
<FIELD ID="4" xsi:type="CharFixed" LENGTH="14"/>
<FIELD ID="5" xsi:type="CharFixed" LENGTH="14"/>
</RECORD>
<ROW>
<COLUMN SOURCE="1" NAME="c1" xsi:type="SQLNCHAR"/>
<COLUMN SOURCE="2" NAME="c2" xsi:type="SQLNCHAR"/>
<COLUMN SOURCE="3" NAME="c3" xsi:type="SQLCHAR"/>
<COLUMN SOURCE="4" NAME="c4" xsi:type="SQLINT" />
<COLUMN SOURCE="5" NAME="c5" xsi:type="SQLINT" />
</ROW>
</BCPFORMAT>
我的询问是这样的:
SET LANGUAGE us_english;
GO
SET DATEFORMAT ymd;
go
BULK INSERT
PROJ.dbo.Costs
FROM C:somewhere est01SAMPLE.DAT
WITH
(
DATAFILETYPE = CHAR ,
FORMATFILE= C:somewhere est01TRANSLATE02.XML ,
ERRORFILE= C:somewhere est01ERRORS.TXT ,
ROWTERMINATOR=
)
GO
When I run this script, I get an overflow error starting with row 2. (That is, row 1 appears to have translated correctly, although I do not see it in the sql table.) A hexdump of ERRORS.TXT reveals that the first error line (row 2) begins with the newline! Of course, that would cause the 4th field to overflow! So it appears the script is not understanding the ROWTERMINATOR. I tried , , , just in case it wasn t seeing the . No avail.
I also attempted a slightly different sql command as per bulk insert txt error with ROWTERMINATOR
并有相同的错误。
对我错过了什么有什么想法吗?
根据请求, 这里是样本. dat 的混血螺旋倾弃处 :
000000: 41 20 20 20 20 20 XX XX 32 30 31 31 31 30 30 31 Q RR20111001
000010: 30 30 30 30 30 30 30 30 30 31 35 30 30 30 30 30 0000000001500000
000020: 30 30 30 30 30 30 31 35 30 30 30 30 0A ZZ 20 20 000000150000.N
000030: 20 20 20 XX XX 32 30 31 31 31 30 30 31 30 30 30 X420111001000
000040: 30 30 30 30 30 30 32 30 30 30 30 30 30 30 30 30 0000002000000000
000050: 30 30 30 32 33 32 30 30 30 0A ZZ 20 20 20 20 20 000232000.Y
请注意 XX 和 ZZ 被掩蔽( 不是真实的数据) 0A 是线性字符, 这是最后0 (hex 30) 和 开始下一行的 ZZ 字符之间唯一的事物。 希望这不会太混乱 。
My solution below works, however, this problem is also discussed here and the solution seems better to me (though I haven t confirmed it, I think I ll try it with the next file). Bulk insert rowterminator issue