Using SQL Server, how do I manually set a given column's value during a Bulk Insert of a CSV file?

StackOverflow https://stackoverflow.com/questions/19661873

  •  01-07-2022
  •  | 
  •  

Question

Background:

I have a collection of CSV files that each have the same data structure. Each file was generated on a given calendar day so, for example, I might have one file from 10/8/13 with 20,000 records, one file from 10/9/13 with 50,000 records, and so on.

All of this CSV data needs to be imported into a SQL Server table, but I have added a column for RecordDate which needs to be set to the value of the day the record was generated.

In total, I have fourteen of these CSV files, so I don't mind running fourteen bulk insert operations like this one:

BULK INSERT CSVTest
FROM 'c:\csvtest.txt'
WITH
(
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)
GO

But each time I do this, I need to set that RecordDate column to the date that the particular CSV I am inserting was generated.

Question:

How do I manually set a given column's value during a bulk Insert of a CSV file?

Was it helpful?

Solution

A simple way would be to add a default constraint to the RecordDate column on the table before each bulk insert is executed (or modify the existing constraint, if there is one). Each inserted row will pick up the default for the RecordDate column (obviously assuming the row doesn't contain a value getting imported into RecordDate). Remove the constraint after the bulk insert is completed.

alter table <Table Name> 
    add constraint <constraint name>
    default <relevant date here> for [RecordDate] 

<run the bulk insert...>

alter table <Table Name>
     drop constraint <Constraint Name>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top