Lajja Thaker
Lajja Thaker

Reputation: 2041

SQLBulk Copy with trigger which fire Update query

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 ?

Upvotes: 1

Views: 197

Answers (1)

AnandPhadke
AnandPhadke

Reputation: 13506

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)

Upvotes: 1

Related Questions