I am trying to learn how to use the MERGE statement. The following code compiles correctly:
ALTER PROCEDURE moto.procPM_UpdateLines @LineId As Int = null, @LineName As Varchar(100), @DeleteMe As Bit = 0 AS BEGIN MERGE moto.tblPMLine AS line USING (SELECT LineId, LineName FROM moto.tblPMLine) AS existsLine ON line.LineId = existsLine.LineId WHEN MATCHED AND @DeleteMe = 1 THEN DELETE WHEN MATCHED AND @DeleteMe = 0 THEN UPDATE SET line.LineName = @LineName WHEN NOT MATCHED THEN INSERT(LineName) VALUES(@LineName); END GO
This is a very simple procedure, I know, but for some reason it does not generate any records when I use the following command.
execute moto.procPM_UpdateLines null, 'First test', 0
Is there any way to find out which of the options it follows, if at all?
The previous saved procedure has been fixed.
MERGE INTO moto.tblPMLine AS T USING (SELECT @LineId as LineId, @LineName as LineName) AS S ON T.LineId = S.LineId WHEN MATCHED AND @DeleteMe = 0 THEN
Now it inserts, updates and deletes, and also returns the result.
source share