Import CSV data into an existing table using a Stored Procedure

Viewed 41

Is it possible to update specific columns from a csv in an existing column from a table using a Stored Procedure?

I have an existing table: dbo.customeremp

It has current values:

emp_id Full_Name Job_Title Promoted TimeStamp
159753 Josh Smith Engineer
159745 Ashley James Admin
158488 Tui Vatu IT

We have a csv file which has the same column names above but has the Promoted values filled. Values: Yes , No

CSV file is located in \\Customer\customeremp.csv

My aim is that if said SP is executed, it picks up the csv file and updates the table mainly the Promoted column and want it so that Timestamp is also filled which returns the date and time of when this the SP is executed:

emp_id Full_Name Job_Title Promoted TimeStamp
159753 Josh Smith Engineer No 2022-09-06 17:50:00
159745 Ashley James Admin Yes 2022-09-06 17:50:00
158488 Tui Vatu IT No 2022-09-06 17:50:00
1 Answers

You can use BULK INSERT to copy data from the file into a temporary table that will be a staging one with all columns in NVARCHAR and then do a query from the temporay table to UPDATE the final table

On BULK INSERT

On UPDATE with joins

Related