I have a table that looks like:
Attributes
------------------------------------
Id | UserId | Key | Value | Active |
I would like to insert a new row into this table, and for every row that has the same UserId and Key, I'd like to set Active to 0.
The merge statement seems like a convenient choice here, but I'm not sure how to perform two actions at once, ie:
MERGE INTO Attributes AS Target
USIN (<values>) AS Source (<columns>)
ON Target.Key = Source.Key AND Target.UserId = Source.Uid
WHEN MATCHED <update, then insert>
WHEN NOT MATCHED <insert>
I could definitely do this in two queries, but I was wondering if there's a way to do what I'm proposing in one.
MERGEdoes not buy you anything here.