简体   繁体   中英

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.

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.

the problem that i was executing the query on sql server management studio 2008 but i was connecting to sql server 2005 database on another server. Now it is fixed

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM