user1666952
user1666952

Reputation: 309

SQL Server BULK INSERT error

This is my source data in CSV format:

4,23,2AY5623,7235623
4,23,2GP1207,1451207
4,23,2GQ6689,4186689

Table:

CREATE TABLE [dbo].[Table1](
    [idCodeLevel] [int] NOT NULL,
    [idFirm] [int] NOT NULL,
    [valCodeFrom] [varchar](15) NOT NULL,
    [valCodeTo] [varchar](15) NOT NULL
) ON [PRIMARY]

This the code I am using to bulk import:

USE Test
GO

TRUNCATE TABLE Table1
GO

BULK INSERT Table1
  FROM 'C:\Temp\test.csv'
  WITH (
    FIELDTERMINATOR = ',',
    MAXERRORS=0,
    ROWTERMINATOR = '\n'
  )
GO

Error I am getting is:

Msg 4864, Level 16, State 1, Line 2
Bulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 1, column 1 (idCodeLevel).

Can you please someone tell me why is it failing?

I googled and found out that I might have to use the format .fmt. But how can I convert a csv file to fmt. I have seen code to create fmt file from sql table.

Thanks a lot for your help!

Upvotes: 1

Views: 7914

Answers (2)

Abhijit
Abhijit

Reputation: 683

use the sql import wizard to import data from external file.

Right click on database--->task--->import----> specify the flat file as source and select the destination server.

for more information please visit Import CSV data to SQL

Upvotes: 0

Russell Fox
Russell Fox

Reputation: 5427

Does the csv have a row at the top of field names? If so you'll need to add "FIRSTROW = 2" to your bulk statement. If not, try creating a new table that is all VARCHAR fields, then check the data: you probably have something strange in your data that you aren't expecting, like a non-printing character. Import as text and then try something like "SELECT ISNUMERIC([FIELD1]) FROM NEWTABLE".

Upvotes: 1

Related Questions