Not sure if I get your question exactly right, but you can use the MERGE
statement in T-SQL as follows:
- If the item in source does not match item in target on specified fields, insert into target.
- If the item in source matches item in target on specified fields, update the other fields in target with corresponding values from source.
In your case, the merge would be something like this:
merge yourtable as target
using
(select value1 a, value2 b
union
select value3 a, value4 b) as source
on target.column1 = source.a
when matched and target.column2 <> source.b
then update
set target.col2 = source.b
when not matched by target
then
insert (column1, column2) values (source.a, source.b);
This example uses hardcoded values, but it works when you are inserting from another table as well.