简体   繁体   English

BULK INSERT 最后一行失败

[英]BULK INSERT fails with last row

I'm importing a CSV using sql server bulk option and below is my sql inputs.我正在使用 sql server 批量选项导入 CSV,下面是我的 sql 输入。

MAXERRORS = 1000000,
CODEPAGE = 1251,
FIELDTERMINATOR = '~%',
ROWTERMINATOR = '0x0a',
ERRORFILE = 'C:\MyFile_BadData.log'

My problem is BULK INSERT fails to load the last row data.我的问题是 BULK INSERT 无法加载最后一行数据。

Also please note that no errors was reported by the sql bulk option..另请注意,sql 批量选项未报告任何错误。

If i add a empty newline to the file the loading works without any issues.如果我在文件中添加一个空的换行符,加载工作没有任何问题。

But my concern is i cannot modify the CSV file, please suggest your valuable inputs if any但我担心的是我无法修改 CSV 文件,如果有的话,请提出您宝贵的意见

This happens when the last line doesn't end with the row terminator.当最后一行不以行终止符结尾时会发生这种情况。 Make sure the last line ends with the row terminator, then the last row will be imported.确保最后一行以行终止符结尾,然后将导入最后一行。

If you can't change the export routine that generates the CSV, use powershell or something to add the row terminator to the CSV.如果您无法更改生成 CSV 的导出例程,请使用 powershell 或其他东西将行终止符添加到 CSV。 If you can't change the original, copy it to a location where you can change it (include that in your powershell script).如果您无法更改原始文件,请将其复制到您可以更改的位置(将其包含在您的 powershell 脚本中)。

Exactly!没错! The last character in the file must be some kind of special character;文件中的最后一个字符必须是某种特殊字符; Line Break, Space, etc. Can you simply delete the last line and do the import?换行符、空格等。你能简单地删除最后一行并进行导入吗? If you need an automated solution, create a small C# app to open the file, delete the last line, and then save the file.如果需要自动化解决方案,请创建一个小型 C# 应用程序来打开文件,删除最后一行,然后保存文件。 THEN, run your import process.然后,运行您的导入过程。 This can all be controlled using the Windows Task Scheduler, so you can even run it as an overnight job, when you are far away from your computer.这一切都可以使用 Windows 任务计划程序进行控制,因此当您远离计算机时,您甚至可以将其作为夜间作业运行。

https://www.digitalcitizen.life/how-create-task-basic-task-wizard https://www.digitalcitizen.life/how-create-task-basic-task-wizard

If you're on a Unix variant, you can also use this to add to the file without having to edit it by hand.如果您使用的是 Unix 变体,您还可以使用它来添加到文件中,而无需手动编辑它。

sed -i '' -e '$a\' fname.csv

Note that this will only add a newline if there isn't one.请注意,如果没有换行符,这只会添加一个换行符。 So running it multiple times will not affect a file that already has a trailing newline.因此多次运行它不会影响已经有尾随换行符的文件。

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

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