Merge Error in SQL Server 2008

I execute the following merge statement in SQL Server 2008:

MERGE PopulationData AS a USING ImagesData AS b ON a.ID = b.ID WHEN MATCHED THEN UPDATE SET a.SURNAME = 'joe123' WHEN NOT MATCHED THEN INSERT(a.ID,a.SURNAME) VALUES (12454,'joe123'); 

I have the following error:

 Msg 156, Level 15, State 1, Line 2 Incorrect syntax near the keyword 'AS'. 

Can someone tell me where the syntax error is.

+4
source share
2 answers

Analyzing your query in SQL Management Studio gives me the following error:

Msg 10739, Level 15, State 1, Line 7 The list of insert columns used in the MERGE statement cannot contain multipart identifiers. Use single part identifiers.

Then I delete the identifiers ...

 MERGE PopulationData AS a USING ImagesData AS b ON a.ID = b.ID WHEN MATCHED THEN UPDATE SET a.SURNAME = 'joe123' WHEN NOT MATCHED THEN INSERT(ID,SURNAME) VALUES (12454,'joe123'); 

... and the request parses successfully. Thus, the syntax error hardly comes from your MERGE statement. Are you really only executing the expression you posted, or is it part of a larger script or procedure? And if you double-click the error message, it should highlight the line where the syntax error is (at least since SQL 2008).

Update: I noticed that you noted a question for SQL 2005 and 2008, but MERGE is only supported in SQL 2008. Parsing a query in SQL 2005 gives a syntax error.

+9
source

The problem is that I was executing a query to SQL Server 2008 Management Studio, but I was connecting to the sql server 2005 database on another server. Now it is fixed

0
source

Source: https://habr.com/ru/post/1381728/


All Articles