SQLBulk Copy with trigger which fire Update query

sql sqlbulkcopy triggers

Question

I have insert records from excel sheet to SQL using SQLBulkCopy.

Code runs fine for me.

But I have to fire Trigger based insertion.

In trigger I am inserting records in another table based on Inserted

that also works fine.

but if I am checking that another table already having that record which I am trying to insert and update that record than that does not works for me.

How can I solve my problem ?

Accepted Answer

Use Merge statement in your Trigger if you are using SQL server 2008

Example:

MERGE INTO dbo.Table1 AS t
USING dbo.inserted AS i
        ON t.id= i.id
WHEN MATCHED THEN
    UPDATE SET
      t.col1= i.col1,
      t.col2= i.col2
WHEN NOT MATCHED THEN 
      INSERT (col1, col2)
      VALUES (i.col1, i.col2)



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