Click here to Skip to main content
15,883,749 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,
I want to move few records from a source table to destination table.(Move-insert into destination table delete from source table) in a single statement.I am reading about merge statement but it is not working properly.

SQL
BEGIN TRAN;
MERGE Target AS T
USING Source AS S
ON (T.EmployeeID = S.EmployeeID) 
WHEN NOT MATCHED BY TARGET 
THEN INSERT(EmployeeID, EmployeeName) VALUES(S.EmployeeID, S.EmployeeName)
WHEN MATCHED
THEN DELETE;
--OUTPUT $action, Inserted.*, Deleted.*;
ROLLBACK TRAN;

Nothing is inserted to target table.
Posted
Updated 16-Sep-14 0:05am
v2

How do you expect to insert some if you ROLLBACK your transaction! You probably meant to END it...
In anyway you better use the single form of DELETE ... OUTPUT ... INTO, it works already as a single transaction, easier to write and easier to read...
http://msdn.microsoft.com/en-us/library/ms177564.aspx[^]
 
Share this answer
 
Comments
souravghosh18 16-Sep-14 6:29am    
if I omitted the tran part it insert the row but not delete it from the source table.
It seems that you are trying to learn merge statement. Check below link that contain similar examples.

Inserting, Updating, and Deleting Data by Using MERGE[^]

Merge Statement in SQL Server 2008[^]
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900