Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8588069
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:43:27+00:00 2026-06-11T22:43:27+00:00

Possible Duplicate: Any way to SQLBulkCopy “insert or update if exists”? I am using

  • 0

Possible Duplicate:
Any way to SQLBulkCopy “insert or update if exists”?

I am using SQLBulkCopy to insert Bulk records

How can I perform on update (rather than an insert) on records that already exist? Is this possible with SQLBulkCopy?

This is my code for SQLBulkCopy

using (var bulkCopy = new SqlBulkCopy(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString, SqlBulkCopyOptions.KeepNulls & SqlBulkCopyOptions.KeepIdentity))
{
    bulkCopy.BatchSize = CustomerList.Count;
    bulkCopy.DestinationTableName = "dbo.tCustomers";
    bulkCopy.ColumnMappings.Clear();
    bulkCopy.ColumnMappings.Add("CustomerID", "CustomerID");
    bulkCopy.ColumnMappings.Add("FirstName", "FirstName");
    bulkCopy.ColumnMappings.Add("LastName", "LastName");
    bulkCopy.ColumnMappings.Add("Address1", "Address1");
    bulkCopy.ColumnMappings.Add("Address2", "Address2");
    bulkCopy.WriteToServer(CustomerList);
}

Application Details

  1. ASP.net MVC 3.0 Razor view Engine
  2. SQL Server 2008
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T22:43:28+00:00Added an answer on June 11, 2026 at 10:43 pm

    Thanks to @pst

    with his suggestions this is how I implemented, if anyone has to implement similar.

    Bulk Insert in to permanent Temp Table

     using (var bulkCopy = new SqlBulkCopy(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString, SqlBulkCopyOptions.KeepNulls & SqlBulkCopyOptions.KeepIdentity))
                {
                    bulkCopy.BatchSize = CustomerList.Count;
                    bulkCopy.DestinationTableName = "dbo.tPermanentTempTable";
                    bulkCopy.ColumnMappings.Clear();
                    bulkCopy.ColumnMappings.Add("CustomerID", "CustomerID");
                    bulkCopy.ColumnMappings.Add("FirstName", "FirstName");
                    bulkCopy.ColumnMappings.Add("LastName", "LastName");
                    bulkCopy.ColumnMappings.Add("Address1", "Address1");
                    bulkCopy.ColumnMappings.Add("Address2", "Address2");
                    bulkCopy.WriteToServer(CustomerList);
                } 
    

    Then call a stored Procedure to Merge the temp table with actual table

     using (Entities context = new Entities())
                {
                   System.Nullable<int> iReturnValue = context.usp_Customer_BulkUploadMerge(customerid, locationID).SingleOrDefault();
                   if (iReturnValue.HasValue)
                   {
                       // return was successful!
                   }
                }
    

    This is how I used Merge in my Stored Procedure

    ALTER PROCEDURE usp_Customer_BulkUploadMerge
        (
          @CustomerID INT ,
          @locationID INT
        )
    AS 
        BEGIN
        DECLARE @retValue INT
            BEGIN TRY
                IF OBJECT_ID('tCustomers') IS NOT NULL 
                    BEGIN
                        BEGIN TRANSACTION MergPatientsTable
                        SET NOCOUNT ON;
                        MERGE dbo.tCustomers AS target
                            USING 
                                ( SELECT    PU.CustomerID ,
                                            PU.LocationID ,
                                            PU.FirstName ,
                                            PU.LastName ,
                                            PU.MiddleInitial ,
                                            PU.Gender ,
                                            PU.DOB
    
                                  FROM      dbo.tPermanentTempTable PU
                                  WHERE     PU.CustomerID = @CustomerID
                                            AND PU.LocationID = @locationID
                                  GROUP BY  PU.CustomerID ,
                                            PU.LocationID ,
                                            PU.FirstName ,
                                            PU.LastName ,
                                            PU.MiddleInitial ,
                                            PU.Gender ,
                                            PU.DOB 
    
                                ) AS source ( CustomerID, LocationID, FirstName,
                                              LastName, MiddleInitial, Gender, DOB )
                            ON ( LOWER(target.FirstName) = LOWER(source.FirstName)
                                 AND LOWER(target.LastName) = LOWER(source.LastName)
                                 AND target.DOB = source.DOB
                               )
                            WHEN MATCHED 
                                THEN 
            UPDATE                SET
                    MiddleInitial = source.MiddleInitial ,
                    Gender = source.Gender,               
                    LastActive = GETDATE()
                            WHEN NOT MATCHED 
                                THEN    
            INSERT  (
                      CustomerID ,
                      LocationID ,
                      FirstName ,
                      LastName ,
                      MiddleInitial ,
                      Gender ,
                      DOB ,
                      DateEntered ,
                      LastActive
                    )             VALUES
                    ( source.CustomerID ,
                      source.LocationID ,
                      source.FirstName ,
                      source.LastName ,
                      source.MiddleInitial ,
                      source.Gender ,
                      source.DOB ,
                      GETDATE() ,
                      NULL
                    );
                        DELETE  PU
                        FROM    dbo.tPermanentTempTable PU
                        WHERE   PU.CustomerID = @CustomerID
                                AND PU.LocationID = @locationID 
                        COMMIT TRANSACTION MergPatientsTable
                        SET @retValue = 1
                        SELECT @retValue
                    END
                ELSE 
                    BEGIN
                    SET @retValue = -1
                        SELECT  @retValue
                    END
            END TRY
            BEGIN CATCH
                ROLLBACK TRANSACTION MergPatientsTable
                DECLARE @ErrorMsg VARCHAR(MAX);
                DECLARE @ErrorSeverity INT;
                DECLARE @ErrorState INT;
                SET @ErrorMsg = ERROR_MESSAGE();
                SET @ErrorSeverity = ERROR_SEVERITY();
                SET @ErrorState = ERROR_STATE();
            SET @retValue = 0
            SELECT @retValue
               -- SELECT  0 AS isSuccess
            END CATCH
        END
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: is there any other way of creating an object without using “new”
Possible Duplicate: Any way to “reboot” the JVM? Is there any option to restart
Possible Duplicate: Is there any way to undo the effects of “git revert head”?
Possible Duplicate: Prevent any form of page refresh using jQuery/Javascript how can i prevent
Possible Duplicate: Any way to make a WPF textblock selectable? Can I make a
Possible Duplicate: How can I obfuscate JavaScript? Hi there, is there any way to
Possible Duplicate: How to create a linux user using C/C++? Is there any way
Possible Duplicate: Using java to create a web browser Is there any way to
Possible Duplicate: Any way to add HttpHandler programatically in .NET? Is there a way
Possible Duplicate: Is there any way to determine text direction from CultureInfo in asp.net?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.