私はここで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;