Hi folks,
I wish to Insert
or Update
a row in a table - so I wish to try and use the MERGE syntax. My problem is that my data (to insert/update) exists in a variable table
. I'm not sure how to write the correct syntax for the insert/update part.
Here's my pseduo code :-
-- Here's the Variable Table ... and not it has not PK.
DECLARE @PersonId INTEGER
DECLARE @variableTable TABLE (
@SomeScore DECIMAL(10,7),
@SomeAverage DECIMAL(10,7),
@SomeCount INTEGER)
-- Insert or Update
MERGE INTO SomeTable
WHERE PersonId = @PersonId
WHEN MATCHED THEN
UPDATE
SET PersonScore = ??????????
PersonAverage = ???????
PersonCount = ????????
WHEN NOT MATCHED THEN
INSERT(PersonId, PersonScore, PersonAverage, PersonCount)
VALUES(@PersonId, ????, ?????, ????)
.. and I'm not sure how I make sure the UPDATE
correctly only updates 1 row (ie... does that need a WHERE
clause?)
Finally, I based my this post on this SO question.