Search code examples
importssms

Import Flat File via SSMS to SQL Server fails


When importing a seemingly valid flat file (csv, text etc) into a SQL Server database using the SSMS Import Flat File option, the following error appears:

Microsoft SQL Server Management Studio

Error inserting data into table. (Microsoft.SqlServer.Import.Wizard)

Error inserting data into table. (Microsoft.SqlServer.Prose.Import)

Object reference not set to an instance of an object. (Microsoft.SqlServer.Prose.Import)

The target table may contain rows that imported just fine. The first row that is not imported appears to have no formatting errors.

What's going wrong?


Solution

  • Check the following:

    • that there are no blank lines at the end of the file (leaving the last line's line terminator intact) - this seems to be the most common issue
    • there are no unexpected blank columns
    • there are no badly escaped quotes

    It looks like the import process loads lines in chunks. This means that the lines following the last successfully loaded chunk may appear to have no errors. You need to look at subsequent lines, that are part of the failing chunk, to find the offending line(s).

    This cost me hours of hair pulling while dealing with large files. Hopefully this saves someone some time.