I have a CSV file with 6 millions rows. Each line is made up of the same format eg/
I,h,q,q,3,A,5,Q,3,[,5,Q,8,c,3,N,3,E,4,F,4,g,4,I,V,9000,0000001-100,G9999999990001800000000000001,G9999999990000001100PDNELKKMMCNELRQNWJ010, , , , , , ,D,Z
I have 2 columns in a table.
The first column should be field 27 in the CSV and the second column should be the whole line in the CSV file.
I have tried to set up a format file but cannot get it working.
Is it even possible to do this sort of mapping?
Here is what I have:
BULK INSERT Staging FROM 'C:\Data.txt'
WITH
(
FIELDTERMINATOR =',',
ROWTERMINATOR ='\n',
KEEPNULLS,
formatfile='C:\format.fmt'
)
This is my format file
9.0
2
1 SQLCHAR 0 40 "," 27 Col27 SQL_Latin1_General_CP1_CI_AS
2 SQLCHAR 0 200 "\r\n" 1 Col1 SQL_Latin1_General_CP1_CI_AS
In terms of comparison, I have this working in SQLite which takes 2min 35secs.
You could crack this nut a couple of ways but the way I did it was to bulk insert the WHOLE csv file into a temp table via dynamic SQL:
Then you can insert the data into the target table like this:
You’ll need to create a parse function like so:
Hope that helps! If you need help with the format file let me know.
Here is the format file contents: