Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import Flat File via SSMS to SQL Server fails

Tags:

import

ssms

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?

like image 446
Andrew Avatar asked May 16 '18 11:05

Andrew


4 Answers

None of these other ones worked for me, however this did:

When you import a flat file, SSMS gives you a brief summary of the data types within each column. Whenever you see a nvarchar that's in an int or double column, change it to int or double. And change all nvarchars to nvarchar(max). This worked for me.

like image 158
Arnav Sharma Avatar answered Sep 20 '22 15:09

Arnav Sharma


If the file you're importing is already open, SSMS will throw this error. Close the file and try again.

like image 23
Kevin Nelson Avatar answered Nov 11 '22 18:11

Kevin Nelson


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.

like image 21
Andrew Avatar answered Nov 11 '22 17:11

Andrew


Make sure when you are creating your flat-file IF you have text (varchar) value in any of your columns, DO NOT select your file to be comma "," delimited. Instead, select vertical line "|" or something that you are SURE it can't be in those values. the comma is supper common to have in nvarchar filed.

I have this issue and none of the recommendations from other answers helped me! I hope this saves someone some times and it took me hours to figure it out!!!

like image 2
Reza Shek Avatar answered Nov 11 '22 16:11

Reza Shek