SQLBulk Copy with trigger which fire Update query

sql sqlbulkcopy triggers

Question

I've used SQL to insert entries from an Excel sheet.SQLBulkCopy .

Code works well for me.

However, I must use trigger-based insertion.

I'm putting entries into another table using a trigger depending onInserted

it is also effective.

However, if I verify that the record I'm attempting to enter and update already exists in another table, then it does not work for me.

How can I get my issue resolved?

1
1
9/21/2012 7:07:58 AM

Accepted Answer

If you are using SQL Server 2008, use the Merge statement in your Trigger.

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)
1
9/21/2012 7:24:54 AM


Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow