hridya pv
hridya pv

Reputation: 1059

Issue with MERGE statement - error in semicolon (SQL Server 2008)

I have done everything i know about MERGE in this fiddle but am not getting output.Hope someone will come up with a solution for this.

I have done a simple MERGE statement to insert, update and delete the values of target table using source table at the same time and everything is fine but am getting error message saying

Error - A MERGE statement must be terminated by a semi-colon (;)

I have put semi-colon but the error keeps popping up.I am studying MERGE statements and how to use them using two table using SQLFiddle and someone please show me the correct approach to place the semi-colon.

Here's the fiddle - SQLFiddle

and here's the code of MERGE

MERGE Students AS T 

USING Teachers AS S
ON S.LastName = T. LastName and
S.FirstName = T.FirstName

WHEN MATCHED THEN
UPDATE SET T.Address = S.Address,
         T.Age = S.Age 

WHEN NOT MATCHED THEN
INSERT (LastName, 
      FirstName, 
      Address, 
      Age) 
      VALUES (S.LastName, 
      S.FirstName, 
      S.Address, 
      S.Age)

WHEN NOT MATCHED BY SOURCE THEN 
DELETE;

OUTPUT $action, Inserted.LastName, Inserted.FirstName, Deleted.LastName, Deleted.FirstName INTO @T;

Select * from @T;

Upvotes: 2

Views: 5962

Answers (1)

Ivan Golović
Ivan Golović

Reputation: 8832

Try this SQL Fiddle, @T definition was wrong, I also removed semicolon after DELETE and changed the query terminator on SQL Fiddle to GO instead of semicolon.

Upvotes: 2

Related Questions