Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: SQL-Server
Hello all,
 
I am working with merge statement to merge two tables but getting error in syntax. Please help me. Thanks in advance.
 

MERGE INTO MERGE1 AS M1
USING (SELECT *FROM MERGE2) AS M2 ON
M1.ID=M2.ID
AND M1.NAME=M2.NAME
AND M1.SALARY=M2.SALARY
AND M1.ADDRESS = M2.ADDRESS
 
WHEN MATCHED THEN
UPDATE
SET M1.ID=M2.ID,
M1.NAME=M2.NAME,
M1.ADDRESS=M2.ADDRESS,
M1.SALARY=M2.SALARY
 
WHEN NOT MATCHED THEN
INSERT
(ID,NAME,SALARY,ADDRESS)
VALUES (M2.ID,M2.NAME,M2.SALARY,M2.ADDRESS)
 

Below are the errors.
 
Error:
 
Msg 156, Level 15, State 1, Line 2
Incorrect syntax near the keyword 'INTO'.
Msg 156, Level 15, State 1, Line 3
Incorrect syntax near the keyword 'AS'.
Posted 4-Jan-13 19:42pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Make sure that the Merge1 table actually exists and that you are running the query against the right database.
 
Note that Merge only works with SQL Server 2008 and 2012.
  Permalink  
Comments
bapu_reddy at 5-Jan-13 2:03am
   
I'm working on SQL server 2008, still getting the same error.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

May be the missing brackets around the ON condition?
 
Try:
MERGE INTO MERGE1 AS M1
USING (SELECT *FROM MERGE2) AS M2 ON
   (M1.ID=M2.ID
    AND M1.NAME=M2.NAME
    AND M1.SALARY=M2.SALARY
    AND M1.ADDRESS = M2.ADDRESS)
 
WHEN MATCHED THEN
UPDATE
SET M1.ID=M2.ID,
M1.NAME=M2.NAME,
M1.ADDRESS=M2.ADDRESS,
M1.SALARY=M2.SALARY
 
WHEN NOT MATCHED THEN
INSERT
(ID,NAME,SALARY,ADDRESS)
VALUES (M2.ID,M2.NAME,M2.SALARY,M2.ADDRESS)
Refer: MSDN: MERGE (Transact-SQL)[^]
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web01 | 2.8.140709.1 | Last Updated 5 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid