我正在尝试学习如何使用 MERGE 运算符。以下代码正确编译:
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
我知道这是一个非常简单的过程,但由于某种原因,当我使用以下命令时,它似乎没有生成任何条目。
execute moto.procPM_UpdateLines null, 'First test', 0
有没有办法让我知道它遵循哪些选项(如果有的话)?
以前的存储过程已修复。
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 --UPDATE
UPDATE SET LineName = @LineName
WHEN MATCHED AND @DeleteMe = 1 THEN --DELETE
DELETE
WHEN NOT MATCHED THEN--INSERT
INSERT (LineName) VALUES (@LineName)
OUTPUT $action AS ChangesMade;
现在它插入、更新和删除,以及返回输出。