简体   繁体   中英

sql bulk insert with additional column

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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM