Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merge statement error in SQL Server 2008

I am executing 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 anyone tell me where the syntax error.

like image 581
Joe123 Avatar asked Jan 18 '23 06:01

Joe123


1 Answers

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

Msg 10739, Level 15, State 1, Line 7 The insert column list used in the MERGE statement cannot contain multi-part identifiers. Use single part identifiers instead.

I then remove 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 query parses successfully. Therefore, the syntax error is almost certainly not coming from your MERGE statement. Are you really executing only the statement 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 with SQL 2008).

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

like image 88
Pondlife Avatar answered Jan 24 '23 20:01

Pondlife