bcp BULK INSERT a quote escaped csv file in SQL Server 2005

Friday, November 20th, 2009

I was having trouble trying to import a quote escaped CSV file into SQL Server 2005 using BULK INSERT. It kept giving me an unexpected EOF error until I used a format file where I created a dummy start and end column to pick up the initial ” and final \r\n.

For example this format file will import six columns which are all formatted as:

“column1″,”column2″,”column3″,”column4″,”column5″,”column6″

9.0
8
1       SQLCHAR       0       1       ""          0     ExtraField          ""
2       SQLCHAR       0       100     "\",\""     1     ModelId             SQL_Latin1_General_CP1_CI_AS
3       SQLCHAR       0       100     "\",\""     2     QualifyModelId      SQL_Latin1_General_CP1_CI_AS
4       SQLCHAR       0       100     "\",\""     3     MediaAspectCode     SQL_Latin1_General_CP1_CI_AS
5       SQLCHAR       0       100     "\",\""     4     SmallImageFilename  SQL_Latin1_General_CP1_CI_AS
6       SQLCHAR       0       100     "\",\""     5     MediumImageFilename SQL_Latin1_General_CP1_CI_AS
7       SQLCHAR       0       100     "\""        6     LargeImageFilename  SQL_Latin1_General_CP1_CI_AS
8       SQLCHAR       0       100     "\r\n"      0     ExtraField           ""