简体   繁体   English

批量插入缺少最后一行?

[英]BULK INSERT missing last row?

I use BULK INSERT for my text files. 我对文本文件使用BULK INSERT。 Everything works fine but one thing that I discovered, If I give the final line's final column a value, it will import. 一切正常,但是我发现了一件事,如果我给最后一行的最后一列一个值,它将导入。 If the value of that final column in the final line is blank, it discards the line, despite the fact that the destination column allows nulls! 如果最后一行中该最后一列的值为空,则尽管目标列允许为空,它也会丢弃该行! Text file uses tab delimiter, here is example of the last row data: 文本文件使用制表符分隔符,这是最后一行数据的示例:

Mike    Johnson 1/29/1987   M

if I have any value in the last column field row will be inserted, example here: 如果我在最后一列中有任何值,将插入行字段,示例如下:

Mike    Johnson 1/29/1987   M   test

This is my BULK Insert: 这是我的批量插入:

BULK INSERT ##TEMP_TEXT
FROM '#uncdir#\#cffile.ServerFile#'
WITH (
    FIELDTERMINATOR = '\t', 
    ROWTERMINATOR = '\n'
)

I tried to use \\r instead of \\n but that did not fix the problem. 我尝试使用\\r代替\\n但这不能解决问题。 I also researched on some websites but could not find any solution. 我也在一些网站上进行了研究,但找不到任何解决方案。 I'm wondering this is something that can be fixed in SQL. 我想知道这是否可以在SQL中修复。 If someone knows how this can be fixed please let me know. 如果有人知道如何解决此问题,请告诉我。

SOLUTION: 解:

For anyone how use ColdFusion here is the line that will add newline in the text file. 对于任何人,如何使用ColdFusion,以下行将在文本文件中添加换行符。

exec xp_cmdshell 'echo. >> "#uncdir#\#cffile.ServerFile#"';

Key was to put double quotes around coldfusion variables, otherwise code does not work. 关键是在Coldfusion变量周围加上双引号,否则代码将无法正常工作。

uncdir code is here: uncdir代码在这里:

<cfset uncdir = createObject("java","java.net.InetAddress").getLocalHost().getHostName()/>

cffile.ServerFile you can get from the form. 您可以从表单获取cffile.ServerFile I used JQuery to submit the text file. 我使用JQuery提交文本文件。 I hope this helps. 我希望这有帮助。 Thank you. 谢谢。

I reproduced your issue on SQL Server 2008 R2. 我在SQL Server 2008 R2上转载了您的问题。 The solution is as simple as adding a newline to your file so that the last row terminates with a newline. 解决方案很简单,只需在文件中添加换行符,以使最后一行以换行符终止。

I created two files: 我创建了两个文件:

  1. without_newline
  2. with_newline

Then ran the following script: 然后运行以下脚本:

CREATE TABLE #t(first_name VARCHAR(128),last_name_etc VARCHAR(128),sex CHAR(1),test VARCHAR(128));

BULK INSERT #t
FROM 'C:\temp\without_newline.txt'
WITH (
    FIELDTERMINATOR='\t',
    ROWTERMINATOR='\n'
);

SELECT * FROM #t;

TRUNCATE TABLE #t;

BULK INSERT #t
FROM 'C:\temp\with_newline.txt'
WITH (
    FIELDTERMINATOR='\t',
    ROWTERMINATOR='\n'
);

SELECT * FROM #t;

DROP TABLE #t;

Result 1: 结果1:

first_name  | last_name_etc     | sex | test
--------------------------------------------
Tom         | Jackson 2/28/1986 | M   | test

Result 2: 结果2:

first_name  | last_name_etc     | sex | test
--------------------------------------------
Tom         | Jackson 2/28/1986 | M   | test
Mike        | Johnson 1/29/1987 | M   | NULL

The solution should be as simple as making sure the last line terminates with \\r\\n . 解决方案应该简单到确保最后一行以\\r\\n结尾。 Either you change the process that generates the text file or do it manually right before you do the bulk insert. 您可以更改生成文本文件的过程,也可以在批量插入之前手动进行操作。

One way to do this manually would be to run EXEC xp_cmdshell 'echo. >> C:\\temp\\without_newline.txt' 手动执行此操作的一种方法是运行EXEC xp_cmdshell 'echo. >> C:\\temp\\without_newline.txt' EXEC xp_cmdshell 'echo. >> C:\\temp\\without_newline.txt' right before you do the bulk insert. EXEC xp_cmdshell 'echo. >> C:\\temp\\without_newline.txt'然后再进行批量插入。

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

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