sql bulk insert with additional column

后端 未结 1 1249
遥遥无期
遥遥无期 2021-01-14 15:56

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 co

相关标签:
1条回答
  • 2021-01-14 16:11

    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.
    0 讨论(0)
提交回复
热议问题