我在这里阅读 MSDN 网站: http ://technet.microsoft.com/en-us/library/bb510625.aspx
我对一件事有点困惑。
该站点的示例(在底部复制)使用该行WHEN NOT MATCHED BY TARGET
。我的问题是,我可以用它在同一个合并中进行插入、更新和删除吗?例如:
--Lazy syntax, but I think you get the idea.
MERGE x AS TARGET USING (ID, [More Fields...]) AS SOURCE
WHEN MATCHED
update
WHEN NOT MATCHED IN TARGET
insert
WHEN NOT MATCHED IN SOURCE
delete
MSDN 示例:
USE AdventureWorks2012;
GO
-- Create a temporary table variable to hold the output actions.
DECLARE @SummaryOfChanges TABLE(Change VARCHAR(20));
MERGE INTO Sales.SalesReason AS Target
USING (VALUES ('Recommendation','Other'), ('Review', 'Marketing'),
('Internet', 'Promotion'))
AS Source (NewName, NewReasonType)
ON Target.Name = Source.NewName
WHEN MATCHED THEN
UPDATE SET ReasonType = Source.NewReasonType
WHEN NOT MATCHED BY TARGET THEN
INSERT (Name, ReasonType) VALUES (NewName, NewReasonType)
OUTPUT $action INTO @SummaryOfChanges;
-- Query the results of the table variable.
SELECT Change, COUNT(*) AS CountPerChange
FROM @SummaryOfChanges
GROUP BY Change;