I have a CSV file containing user info, say
first_name;last_name;user_id;full_name
column separator is ;
, row terminator is \n
.
What I need to do is to insert or update into users
table. Unique key is user_id
: if record with this user_id
already exists, I need to update, if it doesn't I need to insert.
However, there are some problems that prevent me from using management studio data-import or bulk insert
.
First, there are more fields in the users
table (not just 4) and the order of columns in csv
file does not correspond to the order of columns in the table. So I need to be able to specify which column from the file goes to which column in the table.
Secondly, some additional fields need to be filled. For example, users.email = users.user_id
. Here is another obstacle - though for a newly inserted row users.email = users.user_id
it is possible that users.email
will change in the future, so I cannot just insert user_id
and then run update [users] set [email] = [user_id]
.