It works for updating an existing row, but it doesn't insert the row when there's no entry.
This is the CREATE TABLE:
CREATE TABLE [dbo].[Inventory_Update_Hash_Code] ([Product_Id] [int] NOT
NULL, [Feed_Id] [int] NOT NULL, [Hash_Code] [int] NOT NULL,
[Last_Updated] [datetime2](0) NOT NULL
GO
ALTER TABLE [dbo].[Inventory_Update_Hash_Code] ADD PRIMARY KEY
([Product_Id], [Feed_Id])
GO
And this is the query:
MERGE Product_Update_Hash_Code WITH (HOLDLOCK) AS tar
USING (SELECT Feed_Id, Product_Id FROM Product_Update_Hash_Code WHERE
Feed_Id = 261 AND Product_Id = 300) AS source
ON (tar.Feed_Id = source.Feed_Id AND tar.Product_Id = source.Product_Id)
WHEN MATCHED THEN
UPDATE SET tar.Hash_Code = 55, tar.Last_Updated = SYSUTCDATETIME()
WHEN NOT MATCHED
THEN INSERT (Feed_Id, Product_Id, Last_Updated, Hash_Code)
VALUES (261, 300, SYSUTCDATETIME(), 55);
It looks like the "UNMATCHED" clause doesn't get executed. Did I get this wrong?