Skip to content
Advertisement

Import specific values from csv files to column of existing table

I am trying to figure out how to create a query/tsql that can import values from csv file to existing table. This is example how few records of csv looks like:

And my existing table in SQL Employee table All columns already have values except SupervisorId (supervisorId is a foreign key that points at main key of the same table – EmployeeId)

What I want to achieve is to write a script that will take Supervisor name from csv file, search employee table for row with given supervisor name (first name + last name), take his EmployeeId and insert it into his subordinate SupervisorId column.

Is it possible to create such script? So far I have found only simple queries like:

But it is not even close to what I want to achieve

Advertisement

Answer

As @Alex pointed out, the best way to solve this is in a few steps.

Step 1: Getting your data into a #temp table.

Step 2: Do a look up from the Employee table into your loaded data.

Step 3: Update the Employee table with your found new found information!

NOTE: Depending on the size of your data you may want to add a index to your temp tables. In addition joining on name has the chance for row expansion that you may need to contend with.

Good Luck!

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement