tags:

views:

37

answers:

1

Hi,

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!

+1  A: 

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:

  • Determine the name of the extra column and the value to load into it (say, MyDate and 'Jan 1, 1980')
  • Create a (temporary) default on the table based on that (ATLER TABLE MyTable add constraint DF_TempLoad default 'Jan 1, 1980' for MyDate [check the syntax, it may be off]
  • Create a (temporary) view on the table, listing only those columns to be bulk inserted
  • Run the BULK INSERT against the view; the column not included in the view will be assigned the default value
  • Drop the view
  • Drop the default constraint.
Philip Kelley