Carlos Zaiyan
Carlos Zaiyan

Reputation: 1

Merge in sql server 2008 r2

I am executing the following merge statement in SQL Server 2008

MERGE Nuevo_Nav AS a
USING Tabla_correcta AS b
ON a.[No_] = b.[No_ Documento]
WHEN MATCHED THEN
    UPDATE SET a.[Respuesta CAE_CAEC] = b.[Respuesta CAE_CAEC]; 

I have the following error:

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

Upvotes: 0

Views: 177

Answers (2)

CDC
CDC

Reputation: 623

This is a guess, but are you running this in a multi-statement batch/sproc, etc? If so, make sure there is a semi-colon before the beginning of the merge statement. That's a new requirement when the merge statement was introduced in SQL 2008. So:

;MERGE INTO Blah
USING Blah2...
SET Blah.a = Blah2.b;

or

DECLARE @str VARCHAR(1000) = 'This is my previous code line';

MERGE INTO Blah
USING Blah2...
SET Blah.a = Blah2.b;

I forget this every once in a while, since semi-colons after every statement aren't mandatory in most cases.

Upvotes: 1

TTeeple
TTeeple

Reputation: 2989

Like Fireblade said, you don't need a MERGE here. This is a simple UPDATE.

UPDATE a
SET a.[Respuesta CAE_CAEC] = b.[Respuesta CAE_CAEC]
FROM Nuevo_Nav a
INNER JOIN Tabla_correcta b
  ON a.[No_] = b.[No_ Documento]

Upvotes: 0

Related Questions