为什么批量插入错误文件不起作用?

时间:2018-09-04 16:54:23

标签: sql-server error-handling bulkinsert truncate

感谢您的反馈。

H‌ere是我制作的用来强制错误的表。但是除非所有列均为255,否则它不会输入任何内容。这些列接近正确的列大小‌。这是登台表。我希望会创建一个错误文件,但不会创建任何错误文件。

我收到的消息是:

  

消息8152,级别16,状态14,第3行字符串或二进制数据为   被截断。该语句已终止。   ‌

drop TABLE VendorUpc  
CREATE TABLE [dbo].[VendorUpc](
    [ID] [bigint] NOT NULL Identity(1,1) Primary key,
    [VendorNumber] [varchar](8) NULL,
    [VendorName] [varchar](50) NULL,
    [ItemNumber] [varchar](50) NULL,
    [ProductDescription] [varchar](900) NULL, 
    [UOM] [varchar](255) NULL, ---?
    [UPC] [varchar](255) NULL ---? 
) ON [PRIMARY] GO

select * from dbo.VendorUpc

T‌his是批量插入脚本 ‌

truncate table VendorUpc

insert into VendorUpcg
(
    -- ID - This column value is auto-generated
    VendorNumber,
    VendorName,
    ItemNumber,
    ProductDescription,
    UOM,
    UPC    
)
select
    b.VendorNumber,
    b.VendorName,
    b.ItemNumber,
    b.ProductDescription,
    b.UOMs,
    b.UPCs    
from openrowset
(
    bulk 'F:\Data\UPC\Master_File.txt',
    formatfile = 'F:\Data\UPC\Format.xml', 
    errorfile = 'F:\Data\UPC\bulk_insert_BadData.txt', 
    firstrow = 1
) as b

H‌ere是格式文件:

<?xml version="1.0" encoding="utf-8"?>
<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='|' />
  <FIELD ID ="2" xsi:type="CharTerm" TERMINATOR='|' />
  <FIELD ID ="3" xsi:type="CharTerm" TERMINATOR='|'/>
  <FIELD ID ="4" xsi:type="CharTerm" TERMINATOR='|'/>
  <FIELD ID ="5" xsi:type="CharTerm" TERMINATOR='|'/>
  <FIELD ID ="6" xsi:type="CharTerm" TERMINATOR='\n'/> 
</RECORD>
<ROW>
  <COLUMN SOURCE="1" NAME="VendorNumber" xsi:type="SQLVARYCHAR"/>
  <COLUMN SOURCE="2" NAME="VendorName" xsi:type="SQLVARYCHAR"/>
  <COLUMN SOURCE="3" NAME="ItemNumber" xsi:type="SQLVARYCHAR"/>
  <COLUMN SOURCE="4" NAME="ProductDescription" xsi:type="SQLVARYCHAR"/>
  <COLUMN SOURCE="5" NAME="UOMs" xsi:type="SQLVARYCHAR"/>
  <COLUMN SOURCE="6" NAME="UPCs" xsi:type="SQLVARYCHAR"/>
</ROW> 
</BCPFORMAT> ‌

‌‌‌‌ 示例数据:

WT1900|TrueWater|600|"3/4"" Anti-siphon body less union"|Piece|843687114345
WT1900|TrueWater|601|"3/4"" Anti-siphon body with union"|Piece|843687114383
WT1900|TrueWater|602|"1"" Anti-siphon body less union"|Piece|843687114369
WT1900|TrueWater|603|"1"" Anti-siphon body with union"|Piece|843687114406
SUN333|Sunny Bay|604|"3/4"" AVB only"|Piece|843687122029
SUN333|Sunny Bay|605|"1"" AVB only"|Piece|843687122036
SUN333|Sunny Bay|606|"1-1/4"" AV & AVB"|Piece|843687122043
SUN333|Sunny Bay|607|"1-1/2"" AV & AVB"|Piece|843687122050
HD9319|Home Decor|608|"2"" AV & AVB"|Piece|843687122067
HD9319|Home Decor|609|"3/4"" AVU only"|Piece|843687122074

2 个答案:

答案 0 :(得分:0)

您使用的是引用文字。例如

"3/4"" Anti-siphon body less union"

批量复制无法正确处理。

答案 1 :(得分:0)

问题是我需要将MAX_LENGTH添加到我的格式文件中。

现在,即使我说从第一行开始,我在第一行上的行为也很奇怪。我不确定为什么它会将某些文件发送到错误日志。如果我在周围移动它们,则不再将它们发送到错误日志。有点奇怪。但是这部分是固定的。

我希望这对以后的人有所帮助。

<?xml version="1.0" encoding="utf-8"?>
<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" MAX_LENGTH ="8" TERMINATOR='|' />
     <FIELD ID ="2" xsi:type="CharTerm" MAX_LENGTH ="50" TERMINATOR='|' />
     <FIELD ID ="3" xsi:type="CharTerm" MAX_LENGTH ="50" TERMINATOR='|'/>
     <FIELD ID ="4" xsi:type="CharTerm" MAX_LENGTH ="900" TERMINATOR='|'/>
     <FIELD ID ="5" xsi:type="CharTerm" MAX_LENGTH ="255" TERMINATOR='|'/>
     <FIELD ID ="6" xsi:type="CharTerm" MAX_LENGTH ="255" TERMINATOR='\n'/> 
  </RECORD>
  <ROW>
    <COLUMN SOURCE="1" NAME="VendorNumber" xsi:type="SQLVARYCHAR"/>
    <COLUMN SOURCE="2" NAME="VendorName" xsi:type="SQLVARYCHAR"/>
    <COLUMN SOURCE="3" NAME="ItemNumber" xsi:type="SQLVARYCHAR"/>
    <COLUMN SOURCE="4" NAME="ProductDescription" xsi:type="SQLVARYCHAR"/>
    <COLUMN SOURCE="5" NAME="UOMs" xsi:type="SQLVARYCHAR"/>
    <COLUMN SOURCE="6" NAME="UPCs" xsi:type="SQLVARYCHAR"/>
  </ROW>  
</BCPFORMAT>