Determine ROW that caused "unexpected end of file" error in BULK INSERT?

105,646

Solution 1

Fun, fun, fun. I haven't found a good way to debug these problems, so I use brute force. That is, the FirstRow and LastRow options are very useful.

Start with LastRow = 2 and keep trying. Load the results into a throw-away table, that you can readily truncate.

And, you should also keep in mind that the first row could be causing you problems as well.

Solution 2

I have a csv file that i import using Bulk

BULK INSERT [Dashboard].[dbo].[3G_Volume]
FROM 'C:\3G_Volume.csv'
WITH
(
FIRSTROW = 2,
FIELDTERMINATOR = '","',
ROWTERMINATOR = '\n'
)
GO

Usually I used this script and it has no problems but in rare occassions.

I encounter this error..

"The OLE DB provider "BULK" for linked server "(null)" reported an error. The provider did not give any information about the error."

Usually, this happens when the last row have blank values(null).

You need to link your csv file in MS access db to check the data.. (If your csv is not more than 1.4million rows you can open it in excel)

Since my data is around 3million rows I need to use access db.

Then check the number of the last row with blanks and subtract the number of null rows to your total rows for csv.

if you have 2 blank rows at the end and the total number of rows is 30000005 The script will become like this..

BULK
INSERT [Dashboard].[dbo].[3G_Volume]
 FROM 'C:\3G_Volume.csv'
WITH
(
FIRSTROW = 2,
FIELDTERMINATOR = '","',
ROWTERMINATOR = '\n',
Lastrow = 30000003
)
GO

Cheers... Mhelboy

Solution 3

Yeah - BULK INSERT would have done will with a bit more detail in its error messages, and the only way around this is to use the brute force approach, as Gordon rightly pointed out. First, though, based on the error you're getting, it is either not understanding your row terminator, or there is a row terminator missing at the end of the file. Using FIRSTROW and LASTROW will help to determine that.

So, you need to do the following:

  1. Check that there is a row terminator at the end of the file. If not, put one in and try again. Also make sure that the last row contains all of the necessary fields. It it says 'EOF', then that is your problem.
  2. Are you sure there's a LF at the end of each line? Try a CR (\n, 0x0D) and see if that works.
  3. Still not working? Try setting LASTROW=2 and try again. Then try LASTROW=3. If you have more than three rows in your file and this step fails, then the row terminator isn't working.

Solution 4

If CHAR(10) is the row terminator, I don't think you can put it in quotes like you are trying to in BULK INSERT. There is an undocumented way to indicate it, though:

ROWTERMINATOR = '0x0A'

Solution 5

I ran into the same issue. I had written a shell script to create a .csv in Linux. I took this .csv to Windows and tried to bulk load the data. It did not "like" the commas.... Don't ask me why, but I changed to a * as a delimiter in the bulk import and performed a find and replace for comma with * in my .csv .. that worked.. I changed to a ~ as a delimiter, that worked... tab also worked- it didn't like the comma.... Hope this helps someone.

Share:
105,646
Alex Gordon
Author by

Alex Gordon

Check out my YouTube channel with videos on Azure development.

Updated on July 09, 2022

Comments

  • Alex Gordon
    Alex Gordon almost 2 years

    i am doing a bulk insert:

    DECLARE @row_terminator CHAR;
    SET @row_terminator = CHAR(10); -- or char(10)
    
    DECLARE @stmt NVARCHAR(2000);
    SET @stmt = '
      BULK INSERT accn_errors
       FROM ''F:\FullUnzipped\accn_errors_201205080105.txt''
       WITH 
          (
            firstrow=2,
    FIELDTERMINATOR = ''|''  ,
    ROWS_PER_BATCH=10000
       ,ROWTERMINATOR='''+@row_terminator+'''
       )'
    exec sp_executesql @stmt;
    

    and am getting the following error:

    Msg 4832, Level 16, State 1, Line 2
    Bulk load: An unexpected end of file was encountered in the data file.
    Msg 7399, Level 16, State 1, Line 2
    The OLE DB provider "BULK" for linked server "(null)" reported an error. The provider did not give any information about the error.
    Msg 7330, Level 16, State 2, Line 2
    Cannot fetch a row from OLE DB provider "BULK" for linked server "(null)".
    

    is there a way to know on which ROW this error occurred?

    i am able to import 10,000,000 rows without a problem and error occurs after that