BULK INSERT - 二进制数据类型错误
BULK INSERT - error with binary datatype
我正在使用 sqlcmd
将数据写入 text 文件。此数据包含具有 二进制数据类型 的列。这是命令:
sqlcmd -E -Q "' + @queryCommand + '" -o "' + @filePath + '" -s "," -W
现在,我正在使用 BULK INSERT
将该数据导入我的数据库。这是命令(只是重要部分):
N'BULK INSERT ' + @tableName + ' FROM ''' + @importFilePath + ''' WITH
(
ROWS_PER_BATCH = 10000,
TABLOCK,
FIRSTROW = 3,
FIELDTERMINATOR = '','',
ROWTERMINATOR = ''\r\n'',
DATAFILETYPE = ''widenative'',
codepage = ''1251''
)'
对于 DATAFILETYPE
我尝试了 native 和 widenative,对于 ROWTERMINATOR
我试过 0x0a,\r\n 和 \n。我认为 CODEPAGE
在这里无关紧要(我在其他答案中找到了它)因为我也尝试过不使用它。
我得到的错误是:
The bulk load failed. The column is too long in the data file for row 1, column 2. Verify that the field terminator and row terminator are specified correctly.
当我使用正常时 BULK INSERT
:
N'BULK INSERT ' + @tableName + ' FROM ''' + @importFilePath + ''' WITH
(
ROWS_PER_BATCH = 10000,
TABLOCK,
FIRSTROW = 3,
FIELDTERMINATOR = '','',
ROWTERMINATOR = ''\n''
)'
我收到以下错误:
Bulk load data conversion error (truncation) for row 3, column 10 (Column with binary datatype).
顺便说一句,text 文件是这样的:
SomeId,...,SomethingBinary,...
--,------,-----------,-------------,------
11FF47D0-B4A8-452A-9E3F-41BF201C2669,...,0x010005000A741F2B40208AA43B02000000000000,...
OK,花了点时间,但我发现了几个问题:
首先,您的 table 定义(在 SQLFiddle 中)是:
create table [Test]
(
[Id] uniqueidentifier not null,
[SomeValue] int not null,
[BinaryCol] binary not null -- this is binary(1) !!
);
应该是
create table [Test]
(
[Id] uniqueidentifier not null,
[SomeValue] int not null,
[BinaryCol] varbinary(max) not null -- or the appropriate size
);
始终指定数据类型的大小!
其次,我使用了根据正确的 table 定义生成的格式文件:
bcp tmp.dbo.Test format nul -c -x -f C:\temp\Import.xml -t, -T
Output:
<?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="37"/>
<FIELD ID="2" xsi:type="CharTerm" TERMINATOR="," MAX_LENGTH="12"/>
<FIELD ID="3" xsi:type="CharTerm" TERMINATOR="\r\n"/>
</RECORD>
<ROW>
<COLUMN SOURCE="1" NAME="Id" xsi:type="SQLUNIQUEID"/>
<COLUMN SOURCE="2" NAME="SomeValue" xsi:type="SQLINT"/>
<COLUMN SOURCE="3" NAME="BinaryCol" xsi:type="SQLVARYBIN"/>
</ROW>
</BCPFORMAT>
我不得不删除所有第 3 列数据的 '0x'
:
Id,SomeValue,BinaryCol
--,---------,---------
3D30DF1B-D67B-4A2D-B79C-EBDC013928C3,1,010005000A741F2B40208AA43B02000000000000
B896ECB2-39A2-4888-9293-DE382BDBA0B7,2,010005000A741F2B40208AA43B02000000000000
7B053980-DD74-48E3-A348-0567A421E958,3,010005000A741F2B40208AA43B02000000000000
然后我导入了:
BULK INSERT dbo.Test FROM 'C:\temp\res.txt' WITH
(
ROWS_PER_BATCH = 10000,
TABLOCK,
FIRSTROW = 3,
FORMATFILE = 'C:\temp\Import.xml'
)
我正在使用 sqlcmd
将数据写入 text 文件。此数据包含具有 二进制数据类型 的列。这是命令:
sqlcmd -E -Q "' + @queryCommand + '" -o "' + @filePath + '" -s "," -W
现在,我正在使用 BULK INSERT
将该数据导入我的数据库。这是命令(只是重要部分):
N'BULK INSERT ' + @tableName + ' FROM ''' + @importFilePath + ''' WITH
(
ROWS_PER_BATCH = 10000,
TABLOCK,
FIRSTROW = 3,
FIELDTERMINATOR = '','',
ROWTERMINATOR = ''\r\n'',
DATAFILETYPE = ''widenative'',
codepage = ''1251''
)'
对于 DATAFILETYPE
我尝试了 native 和 widenative,对于 ROWTERMINATOR
我试过 0x0a,\r\n 和 \n。我认为 CODEPAGE
在这里无关紧要(我在其他答案中找到了它)因为我也尝试过不使用它。
我得到的错误是:
The bulk load failed. The column is too long in the data file for row 1, column 2. Verify that the field terminator and row terminator are specified correctly.
当我使用正常时 BULK INSERT
:
N'BULK INSERT ' + @tableName + ' FROM ''' + @importFilePath + ''' WITH
(
ROWS_PER_BATCH = 10000,
TABLOCK,
FIRSTROW = 3,
FIELDTERMINATOR = '','',
ROWTERMINATOR = ''\n''
)'
我收到以下错误:
Bulk load data conversion error (truncation) for row 3, column 10 (Column with binary datatype).
顺便说一句,text 文件是这样的:
SomeId,...,SomethingBinary,...
--,------,-----------,-------------,------
11FF47D0-B4A8-452A-9E3F-41BF201C2669,...,0x010005000A741F2B40208AA43B02000000000000,...
OK,花了点时间,但我发现了几个问题:
首先,您的 table 定义(在 SQLFiddle 中)是:
create table [Test]
(
[Id] uniqueidentifier not null,
[SomeValue] int not null,
[BinaryCol] binary not null -- this is binary(1) !!
);
应该是
create table [Test]
(
[Id] uniqueidentifier not null,
[SomeValue] int not null,
[BinaryCol] varbinary(max) not null -- or the appropriate size
);
始终指定数据类型的大小!
其次,我使用了根据正确的 table 定义生成的格式文件:
bcp tmp.dbo.Test format nul -c -x -f C:\temp\Import.xml -t, -T
Output:
<?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="37"/>
<FIELD ID="2" xsi:type="CharTerm" TERMINATOR="," MAX_LENGTH="12"/>
<FIELD ID="3" xsi:type="CharTerm" TERMINATOR="\r\n"/>
</RECORD>
<ROW>
<COLUMN SOURCE="1" NAME="Id" xsi:type="SQLUNIQUEID"/>
<COLUMN SOURCE="2" NAME="SomeValue" xsi:type="SQLINT"/>
<COLUMN SOURCE="3" NAME="BinaryCol" xsi:type="SQLVARYBIN"/>
</ROW>
</BCPFORMAT>
我不得不删除所有第 3 列数据的 '0x'
:
Id,SomeValue,BinaryCol
--,---------,---------
3D30DF1B-D67B-4A2D-B79C-EBDC013928C3,1,010005000A741F2B40208AA43B02000000000000
B896ECB2-39A2-4888-9293-DE382BDBA0B7,2,010005000A741F2B40208AA43B02000000000000
7B053980-DD74-48E3-A348-0567A421E958,3,010005000A741F2B40208AA43B02000000000000
然后我导入了:
BULK INSERT dbo.Test FROM 'C:\temp\res.txt' WITH
(
ROWS_PER_BATCH = 10000,
TABLOCK,
FIRSTROW = 3,
FORMATFILE = 'C:\temp\Import.xml'
)