从CSV到SQL Server的BULK INSERT导致错误

时间:2017-01-15 13:24:39

标签: sql-server csv import

我有CSV格式的简单表格:

999,"01/01/2001","01/01/2001","7777777","company","channel","01/01/2001"
990,"01/01/2001","01/01/2001","767676","hhh","tender","01/01/2001"
3838,"01/01/2001","01/01/2001","888","jhkh","jhkjh","01/01/2001"
08987,"01/01/2001","01/01/2001","888888","hkjhjkhv","jhgjh","01/01/2001"
8987,"01/01/2001","01/01/2001","9999","jghg","hjghg","01/01/2001"
jhkjhj,"01/01/2001","01/01/2001","9999","01.01.2001","hjhh","01/01/2001"
090009,"","","77777","","","01/01/2001"
980989,"01/01/2001","01/01/2001","888","","jhkh","01/01/2001"
0000,"01/01/2001","01/01/2001","99999","jhjh","","01/01/2001"
92929,"01/01/2001","01/01/2001","222","","","01/01/2001"

我正在尝试使用BULK INSERT(Transact-SQL)将数据导入SQL Server

set dateformat DMY;

BULK INSERT Oracleload
FROM '\\Mac\Home\Desktop\Test\T_DOGOVOR.csv'
WITH
    (FIELDTERMINATOR = ',',
     ROWTERMINATOR = '\n',
     KEEPNULLS);

在输出中我得到下一个错误:

  

Msg 4864,Level 16,State 1,Line 4
  第1行第2列(date_begin)的批量加载数据转换错误(类型不匹配或指定代码页的无效字符)....

日期格式可能有问题。但是我需要写什么脚本来修复这个错误?

请帮忙。

提前致谢。

3 个答案:

答案 0 :(得分:1)

BULK INSERT(也不是bcp)无法(正确)处理CSV文件specially if they have (correctly) " quotes。替代方案是SSIS或PowerShell

答案 1 :(得分:0)

我总是在Notepad ++中查看数据,看看是否有一些奇怪的字符,或者是不可打印的字符,比如换行符等等。为此,您似乎可以使用记事本打开它(如果您没有Notepad ++)执行查找替换“没有...保存文件,并重新执行批量加载。

答案 2 :(得分:0)

此记录:

  

jhkjhj," 01/01/2001"" 01/01/2001"" 9999"" 2001年1月1日和#34; " hjhh"" 01/01/2001"

第一列有某种数字类型。您无法将jhkjhj值放入该字段中。

此外,某些记录在日期字段中具有空值("")。这些可能被解释为空字符串,而不是null日期,而不是正确转换。

但错误是指"第1行,第2列和第34行。这就是这个价值:

  

" 01/01/2001"

同样,导入将此解释为字符串,而不是日期。我怀疑它试图导入引号(")而不是仅仅将它们用作分隔符。

您可以尝试批量加载到特殊保留表,然后从那里重新导入。或者,您可以更改数据的导出方式或编写程序以对其进行预清理 - 从不应该使用它们的字段中删除引号,隔离具有不会插入到异常文件的数据的记录,以及报告。