Link to home
Start Free TrialLog in
Avatar of Manju
ManjuFlag for India

asked on

Unable to import csv using Bulk Insert

Team - I am unable to import CSV using bulk insert. Kindly help.

Regards,
Manju

Query to import:

BULK
INSERT INP_Daily_NNB
FROM 'C:\Test1.csv'
WITH
(
FIRSTROW = 2,
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n',
TABLOCK
)
GO
Test1.csv
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

and the error message is what?
Avatar of Manju

ASKER

no error message as such. just 0 rows affected.
please clarify: the file  is on the same box as the sql server instance?
normally, that would only be possible if the file is "empty", or all records skipped ...

please try to add this inside the WITH ( ... ) option block:
, ERRORFILE = 'C:\Test1.log'

and see (post) the  resulting file content
Avatar of Manju

ASKER

File has data & yes, in the same instance. Im unable to create the log file as I dont have admin access in C:.

Please help. no data is getting saved in the table.
you should then check with the admins of that box, so that you get another location where you can store the .log file ...
C:\ is by the way not a really nice folder, anyway...
you will need that log file to see what is happening
can you also post the table creation script, please?
Avatar of Manju

ASKER

I wont be able to get admin access.., nonetheless, Table creation query below


/****** Object:  Table [dbo].[INP_Daily_NNB]    Script Date: 06/16/2016 19:49:58 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[INP_Daily_NNB](
	[CBD Organization_All organizations (ID)] [nvarchar](255) NULL,
	[CBD Organization_All organizations (Long Name)] [nvarchar](255) NULL,
	[CBD Organization_Level 2 (ID)] [nvarchar](255) NULL,
	[CBD Organization_Level 2 (Long Name)] [nvarchar](255) NULL,
	[CBD Organization_Level 3 (ID)] [nvarchar](255) NULL,
	[CBD Organization_Level 3 (Long Name)] [nvarchar](255) NULL,
	[CBD Organization_Level 4 (ID)] [nvarchar](255) NULL,
	[CBD Organization_Level 4 (Long Name)] [nvarchar](255) NULL,
	[CBD Organization_Level 5 (ID)] [nvarchar](255) NULL,
	[CBD Organization_Level 5 (Long Name)] [nvarchar](255) NULL,
	[CBD Organization_Level 6 (ID)] [nvarchar](255) NULL,
	[CBD Organization_Level 6 (Long Name)] [nvarchar](255) NULL,
	[Customer_All Customers (ID)] [nvarchar](255) NULL,
	[Customer_All Customers (Long Name)] [nvarchar](255) NULL,
	[Customer_Customer Groupings (ID)] [nvarchar](255) NULL,
	[Customer_Customer Groupings (Long Name)] [nvarchar](255) NULL,
	[Customer_Top Customer Level (ID)] [nvarchar](255) NULL,
	[Customer_Top Customer Level (Long Name)] [nvarchar](255) NULL,
	[Customer_Customer Level 2 (ID)] [nvarchar](255) NULL,
	[Customer_Customer Level 2 (Long Name)] [nvarchar](255) NULL,
	[Customer_Customer Level 3 (ID)] [nvarchar](255) NULL,
	[Customer_Customer Level 3 (Long Name)] [nvarchar](255) NULL,
	[Customer_Customer Level 4 (ID)] [nvarchar](255) NULL,
	[Customer_Customer Level 4 (Long Name)] [nvarchar](255) NULL,
	[Customer_Customer Level 5 (ID)] [nvarchar](255) NULL,
	[Customer_Customer Level 5 (Long Name)] [nvarchar](255) NULL,
	[Product_Total Products (ID)] [nvarchar](255) NULL,
	[Product_Total Products (Long Name)] [nvarchar](255) NULL,
	[Product_Sector (ID)] [nvarchar](255) NULL,
	[Product_Sector (Long Name)] [nvarchar](255) NULL,
	[Product_SubSector (ID)] [nvarchar](255) NULL,
	[Product_SubSector (Long Name)] [nvarchar](255) NULL,
	[Product_Category (ID)] [nvarchar](255) NULL,
	[Product_Category (Long Name)] [nvarchar](255) NULL,
	[Product_Brand (ID)] [nvarchar](255) NULL,
	[Product_Brand (Long Name)] [nvarchar](255) NULL,
	[Product_Segment (ID)] [nvarchar](255) NULL,
	[Product_Segment (Long Name)] [nvarchar](255) NULL,
	[SU Net New Business] [float] NULL
) ON [PRIMARY]

GO

Open in new window

got it to work with:
ROWTERMINATOR = '0x0a',

the \n is not correct, as on windows platforms this actually refers implicitly to \r\n ...
Avatar of Manju

ASKER

Im getting the below error while executing with the above change.

Msg 4864, Level 16, State 1, Line 1
Bulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 2, column 39 (SU Net New Business).
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Manju

ASKER

Thank you
Avatar of Member_2_7965317
Member_2_7965317

Hi
The CSV files are comma separated files
so in the .csv files  columns data should not have comma

in your file few columns data having  comma in text  some sample columns
CBD Organization_Level 4 (Long Name)
CBD Organization_Level 5 (Long Name)
...
etc

i have replaced the comma with space and tried to do bulk insert it  works fine
please try same way, it works