简体   繁体   English

批量插入字段映射不正确

[英]Bulk insert field mapping incorrect

I'm trying to do a bulk insert. 我正在尝试进行批量插入。

Here is my sql script 这是我的SQL脚本

GO
Use test
DROP TABLE PDTEST
create table pdtest
(
 CustID INT IDENTITY(1,1),
 work_phone_extension varchar(5),
 residential_postal_or_zip_code varchar(30),
 residential_street_address_line_1 varchar(30),
 residence_phone varchar(17),
 work_phone varchar(17),
 name_part varchar(30),
 mailing_city varchar(20),
 mailing_postal_or_zip_code varchar(30),
 mailing_street_address_line_2 varchar(30),
 mailing_street_address_line_1 varchar(30),
 cell_phone varchar(17),
 residential_city varchar(20),
 residential_country_and_province_or_state varchar(10),
 mailing_country_and_province_or_state varchar(10)
)
BULK INSERT pdtest
FROM 'C:\Python27\Scripts\pd.csv'
WITH
(
 FIELDTERMINATOR = ',',
 ROWTERMINATOR = '\n',
 KEEPNULLS,
 FIRSTROW = 2
)
GO

Here is my textfile row 这是我的文本文件行

,91234,,,,WOLFIE HOWLETT,416-,Toronto,,,,,Toronto,CA_ON,CA_ON

All my columns are messed up.The name part should have Wolfie Howlett. 我所有的专栏都搞砸了,名字部分应该有Wolfie Howlett。 It has 416- and then the work phone extension should be null but it has got value 91234 and residential_postal_or_zip_code is null which is wrong. 它具有416-,然后工作电话分机应该为空,但其值为91234,而resident_postal_or_zip_code为null,这是错误的。

The field mapping is wrong here.What should I do to correct it? 此处的字段映射不正确。我该怎么做才能更正它?

So your problem is you have a comma as your first character, so SQL is treating that empty space before it as a column value and inserting that into CustID, so everything is right-shifted by one. 因此,您的问题是,您的第一个字符是逗号,因此SQL会将空格之前的空白视为列值,并将其插入CustID中,因此所有内容都右移了一个。 So either create a dummy column in your test table before CustID, or remove the first comma in your rows. 因此,要么在测试表中的CustID之前创建一个虚拟列,要么删除行中的第一个逗号。

sorry to say but you have to edit your text file. 抱歉,您必须编辑文本文件。 It is creating a syntax error, so has to be modified. 它正在创建语法错误,因此必须进行修改。 I can suggest you way that you can remove and add new text file, with same name and at same location. 我可以建议您使用相同的名称和位置删除和添加新的文本文件。

Seeing your syntax, it seems you had given 'auto increment' to your first column. 看到语法后,您似乎已在第一列中设置了“自动递增”。 So, you need not have to provide value for CustID, and no need of keeping that starting comma in csv file. 因此,您不必提供CustID的值,也不需要将起始逗号保留在csv文件中。

Sql will take CustId value by its own. Sql将自己获取CustId值。 Provide below syntax for csv file 为csv文件提供以下语法

91234,,,,WOLFIE HOWLETT,416-,Toronto,,,,,Toronto,CA_ON,CA_ON

Hope it will help ! 希望对您有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM