批量插入类型不匹配或无效字符

时间:2017-08-31 18:21:50

标签: sql-server csv tsql ssms bulkinsert

我搜索过许多批量插入类型不匹配的SO问题但尚未找到解决我的确切问题的问题。我正在将CSV批量插入本地SSMS数据库,但是我一直收到错误:

' (type mismatch or invalid character for the specified codepage) for row 2, 
   column 4 (DayNumber) '

重复此错误,直到SSMS达到最大错误数。我以前见过这个错误,并且始终能够完成它。此外,我已经尝试了SO上列出的所有标准修复,但没有任何运气。关于这个可能导致某人能够帮助我的特定错误,有三件事可以告诉我:

1) DayNumber is actually column 5 in the underlying CSV, not column 4
2) The DayNumber column simply has numeric values, I even with through row 
   by row to ensure there were no irregular characters contained in the 
   column
3) I have tried bulk inserting the CSV with this column set as a varchar, 
   int, numeric, and float - all of which produce the same error

对此的任何帮助都将非常感激,因为这会阻碍整个项目。此外,我成功地将2个不同的CSV文件批量插入到同一查询中。这些CSV文件存储在与导致错误的文件完全相同的文件夹中。

提前感谢您提供的任何帮助!

以下示例数据:

示例CSV数据:

Country     Region       Month     Date       DayNumber  Location
USA         California   January   8/17/2016    48       USA
England     London       March     6/30/2017    91       England
USA         Wyoming      November  3/31/2017    90       USA
Canada      Alberta      January   12/31/2016   92       Canada
England     Manchester   February  9/30/2016    92       England
New Zealand Auckland     July      6/30/2016    91       New Zealand
USA         New York     December  3/31/2016    91       USA
Canada      Ontario      April     12/31/2015   92       Canada
USA         New York     September 9/30/2015    92       USA
Australia   Queensland   January   6/30/2015    91       Australia
Australia   Sydey        March     3/31/2015    90       Australia
USA         San Diego    February  12/31/2014   92       USA
Canada      Quebec       October   9/30/2014    92       Canada
USA         Florida      August    6/30/2014    91       USA

示例批量插入声明:

Create Table Table_A
 (
   [Country] varchar(max),
   [Region] varchar(max),
   [Month] varchar(max),
   [Date] varchar(max),
   [DayNumber] varchar(max),
   [Location] varchar(max),
                                )
  GO

BULK INSERT
Table_A
FROM 'C:\Path\CSV.csv'
WITH

(
    FIELDTERMINATOR = ',',
    ROWTERMINATOR = '\n',
    FIRSTROW = 2
);

1 个答案:

答案 0 :(得分:0)

here

如果您格式化这样的数据,它将起作用。打开'显示所有字符'在记事本++中,确保字段之间没有隐藏字符(仅在每个记录的末尾)。