Skip to content
Advertisement

SQL UPSERT QUERY W/ Duplicate Rows Made Unique Based Upon 3 Fields (C# VisStudio)

Background is I’m making a SQL connection that takes a .csv file and imports it into a SQL Server database table.

The problem I’m running into is, I’m having trouble with the query syntax because there is no unique identifier for a row in the .csv file I’m importing. It takes 3 fields combined to makes a row unique/distinct.

Rough example of the .csv files’ data, the first three of the .csv columns can be considered together to make unique rows:

This is my query syntax simplified in Visual Studios, (I’m importing 25 or so columns in reality from the .csv file, and so to keep things straight/simple I made the columns names the exact same in both the .csv file and the SQL-Server table), but the basic syntax looks like this:

Once imported I see a few problems in the SQL Server table,

  1. The order_id field will be null
  2. It’s only importing a very small amount of the data, about 50 of 2000 records
  3. If I re-import the data with a change in the .csv file, say with a single new row, I get 100 of 2000 records

I’m not sure if what I’m trying to do is possible or worth it. Should I be breaking this down a lot more instead of doing it all in one query? I’m not necessarily new to coding but I don’t code very often/I’m rusty and this is my first C# project so lend me some patience if possible.

Just wanted to add more code in response to @casey crookston, it’s possible problems 2 and 3 are related to my loop

Advertisement

Answer

This looks like a good spot to use SQL Server’s MERGE syntax:

This uses the first 3 columns as primary key; when a tuple already exists, then colums other_1 and other_2 are updated with the values that would have been otherwise inserted.

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