SQL merge not matched by target vs not matched by source

Use caution, as you may need to further qualify the WHEN NOT MATCHED BY SOURCE.

For example, if the TARGET table has a column that the SOURCE does not .. and you are setting that target column during the aforementioned insert .. then you'll likely want to define that constraint:

WHEN NOT MATCHED BY SOURCE AND (TARGET.SomeColumn = yada)


WHEN NOT MATCHED BY TARGET - You should use this clause to insert new rows into the target table. The rows you insert into the table are those rows in the source table for which there are no matching rows in the target.

WHEN NOT MATCHED BY SOURCE - If you want to delete a row from the target table that does not match a row in the source table