I want to create a merge that will compare two tables and insert not matched values into another third table or table variable
something like this:
MERGE Assets AS target
USING (@id, @name)FROM Sales AS source (id, name) ON (target.id = SOURCE.id)
WHEN MATCHED THEN
UPDATE SET target.Status = @status, target.DateModified = SYSUTCDATETIME()
WHEN NOT MATCHED THEN
INSERT INTO @tableVar (id, name, status, dateModified)
VALUES (@id, @name, @status, SYSUTCDATETIME())
Can this be done or are there other methods?
You just cannot do this.
MERGEoperates on two tables only – source and target.For your requirement, you need to e.g. use a CTE (Common Table Expression) to find the rows that don’t match – and insert those into the third table.
Something like: