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

后端 未结 6 2066
青春惊慌失措
青春惊慌失措 2020-11-28 09:10

I need to update a very large table periodically and SQLBulkCopy is perfect for that, only that I have a 2-columns index that prevents duplicates. Is there a way to use SQLB

6条回答
  •  悲&欢浪女
    2020-11-28 09:53

    Got a hint from @Ivan. For those who might need, here's what I did.

    create trigger yourschma.Tr_your_triger_name
        on yourschma.yourtable
        instead of INSERT
        as
        merge into yourschma.yourtable as target
        using inserted as source
        on (target.yourtableID = source.yourtableID)
        when matched then
            update
            set target.ID     = source.ID,
                target.some_column = source.some_column,
                target.Amount                       = source.Amount
        when not matched by target then
            insert (some_column, Amount)
            values (source.some_column, source.Amount);
    go
    

提交回复
热议问题