Bulk insert into existing table - only insert new rows

c# sqlbulkcopy sql-server

Question

I've got a method to get data from existing table in database as DataTable and i want to add new rows to that datatable and then use SQLBulkCopy to only bulk insert the newly added rows and not the already existing rows, do i need to configure this somehow or will it only add the new rows anyway?

var myDataTable = GetExistingTableDataAsDataTable();

//add new rows to myDataTable.....

using (var conn = new SqlConnection("Connection string"))
    {
        conn.Open();

        using (var bulk = new SqlBulkCopy(conn))
        {
            bulk.DestinationTableName = myDataTable.TableName;
            bulk.BatchSize = 1000;

            foreach (var column in myDataTable.Columns)
            {
                bulk.ColumnMappings.Add(column.ToString(), column.ToString());   
            }

            bulk.WriteToServer(myDataTable); //Do i need to add DataRowState.Added here?
            bulk.Close();
        }

        conn.Close();
    }

Popular Answer

Use a temporary table to bulk insert. Then, run a query to copy new rows over, filtering out duplicates with a where clause:

insert  YourTable
        (id, col1, col2, col3, ...)
select  id
,       col1
,       col2
,       col3
,       ...
from    TempTable t
where   not exists
        (
        select  *
        from    YourTable yt
        where   yt.id = t.id
        )


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why