Please take a look at the formatting of this MERGE statement:
I see three issues her, two major, one minor:
- (Major) The DELETE WHERE part of the WHEN MATCHED THEN UPDATE part is completely wrong!
It must be inline with the UPDATE statement. - (Major) The WHERE clause of the WHEN NOT MATCHED THEN INSERT part must be inline with the INSERT statement.
- (Minor) The field and value list of the Insert part could be one item per line.