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

后端 未结 11 1959
闹比i
闹比i 2020-12-16 10:18

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 I         


        
相关标签:
11条回答
  • 2020-12-16 10:40

    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.
    0 讨论(0)
  • 2020-12-16 10:42

    To locate the troublesome row use the errorfile specifier.

    BULK INSERT myData
    FROM 'C:\...\...\myData.csv'
    WITH (
    FIELDTERMINATOR = ',',
    ROWTERMINATOR = '\n',
    ERRORFILE = 'C:\...\...\myRubbishData.log' 
    );
    

    myRubbishData.log will have the offending rows and a companion file myRubbishData.log.txt will give you row numbers and offsets into the file.

    Companion file example:

    Row 3 File Offset 152 ErrorFile Offset 0 - HRESULT 0x80004005
    Row 5 File Offset 268 ErrorFile Offset 60 - HRESULT 0x80004005
    Row 7 File Offset 384 ErrorFile Offset 120 - HRESULT 0x80004005
    Row 10 File Offset 600 ErrorFile Offset 180 - HRESULT 0x80004005
    Row 12 File Offset 827 ErrorFile Offset 301 - HRESULT 0x80004005
    Row 13 File Offset 942 ErrorFile Offset 416 - HRESULT 0x80004005
    
    0 讨论(0)
  • 2020-12-16 10:44

    In my experience this is almost always caused by something in the last two lines. tail the import file and it should still give you the failure. Then open it in a full text editor that lets you see non-printing characters like CR, LF, and EOF. That should enable you to kludge it into working, even if you don't know why. E.g., BULK INSERT fails with row terminator on last row

    0 讨论(0)
  • 2020-12-16 10:44

    I got around the problem if I converted all fields to string and then used a common fielddelimiter.

    0 讨论(0)
  • 2020-12-16 10:46

    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.

    0 讨论(0)
提交回复
热议问题