Question

I try to do a bulk copy from a file into a table in sqlserver 2008. Its only a part of the file that I want to copy (in the BCP command example bellow it is only line number 3 I will copy).

Sometimes I receive the "Unexpected EOF encountered" error message.

The command is: BCP tblBulkCopyTest in d:\bc_file.txt -F3 -L3 -c -t, -S(local) -Utest -Ptest

When the file looks like the following, the copy works fine (the line number 3 is inserted into my table):

44261233,207,0,0,2168  
44261234,207,0,0,2570  
44261235,207,0,0,2572  

When the file looks like the following, I receive "Unexpected EOF encountered" error message:
Test
44261233,207,0,0,2168
44261234,207,0,0,2570
44261235,207,0,0,2572

It seems like when the file starts with something not in the correct format, the BCP command fails, even though it is not the line I want to copy (in the bcp command I have specified line number 3).

When the file looks like the following, the copy works fine:

44261233,207,0,0,2168  
44261234,207,0,0,2570  
44261235,207,0,0,2572 
Test  

So it is only when the file has some incorrect data "before" the lines I want to copy, that I receive the error.

Any suggestions how the BCP command will ignore the lines, not in question?

Was it helpful?

Solution

The way I solve that kind of errors every day:

1) Create a table with single column tblRawBulk(RawRow varchar(1000)).

2) Insert all of the rows there.

3) Remove unnecessary unformatted rows (e.g. 'test' in your example) with WHERE-parameter. Or even remove all unnecessary rows (except of rows, that you need to load) to simplify point 5.

4) Upload this table with BCP to some workfolder.

5) Download this new file.

It's no exact what you want, but may help if you'll write corresponding stored procedure, that can automatically do all this things.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top