I have a temporary table with some data. I have another table with the name
Employee_Master. Employee data regularly comes in an Excel sheet and this data is
passed as XML to a stored procedure. In the stored procedure, this XML data is
read and the above mentioned temporary table is filled.
Both, temporary table and Employee_Master, have Employee_Id as common field. Employee_Master has Employee_ID Primary Key.
I now want to insert records from the temporary table to Employee_Master table.
If Employee_Id exists in Employee_Master table, the row in this table should be
updated with values from the temporary table else a new row should be inserted.
To achieve this I am using MERGE statement like this:
MERGE Employee_Master EM USING #SheetData SD ON EM.Employee_Id = SD.Employee_Id WHEN MATCHED THEN UPDATE SET ActivityStatus = SD.ActivityStatus, Salary = SD.Salary WHEN NOT MATCHED THEN INSERT (ActivityStatus, Salary) VALUES (SD.ActivityStatus,SD.Salary);I have few questions on this:
(1) Is MERGE the right choice for this?
(2) While Updating and Inserting with MERGE, I want to keep track of what all IDs
are updating and inserting. How to use OUTPUT clause within MERGE?
(3) How MERGE works? Does it work row by row matching the join condition?