Totally reedited this after some thought. Originally I was hoping to combine with a separate MERGE statement, but forget that. To be clear this is not an upsert. I want to add a new record if an existing, matching record is not found.
Let's say mytable
holds columns foo
, bar
, baz
. Where (if relevant for performance sake) foo
and bar
together uniquely identify the record.
a) If not foo
and bar
then insert foo
, bar
, baz
b) Else, if foo
and bar
but baz<new>
does not equal baz<old>
insert foo
, bar
, baz<new>
c) Else if all three match then do nothing
Note that b
is likewise a new record not an update. Essentially this is a log recording changes to baz
.
As an added bonus, it would be great if I could get the MERGE statement (to my_second_table
) to only attempt a merge on a
and b
since case c
means baz
is unchanged therefore we don't need to touch the other table. But I know you can't have everything.
Why can't they just use JavaScript like Mongo does...?