Reputation: 66555
A csv file contains 8 columns (col1, col2, ..., col8) and the name of the file contains the date which has to be inserted into the table as well.
If the number of columns in the table and columns in the csv file are equal the following query imports all the records from the file to the table:
query += "BULK INSERT real_data FROM '" + path + "' WITH (FIELDTERMINATOR = ',', ROWTERMINATOR = '\n')";
So far I haven't found a solution to modify the query such that the new records can contain the date extracted from the filename. Anyway I have created a function to extract the date:
DateTime eventTime = extractDate(path);
and would like to insert eventTime into 9th column for each record imported from file.
Does anyone know how to modify/create query statement to import 8 columns from file and add the date as 9th column for each imported record?
Thank you!
Upvotes: 4
Views: 9298
Reputation: 40289
You cannot add an "arbitrary column" to the data set being loaded with the BULK INSERT command. (SSIS packages can do that, if you want to deal with their complexity.)
The following trick is a bit complex, but I've used it succesfully several times:
Upvotes: 6