CREATE TABLE FarmAnimals (AnimalID int IDENTITY PRIMARY KEY, AnimalName nvarchar(50), Price money) GO
CREATE TABLE Pets (AnimalID int IDENTITY PRIMARY KEY, AnimalName nvarchar(50), Price money) GO
INSERT FarmAnimals (AnimalName, Price) VALUES (′Goat′, 250),(′Sheep′, 300) GO
INSERT Pets (AnimalName, Price) VALUES (′Kitten′, 75),(′Puppy′, 120), (′Goat′, 350) GO
MERGE INTO Pets USING FarmAnimals ON Pets.AnimalName = FarmAnimals.AnimalName WHEN MATCHED THEN UPDATE SET Price = FarmAnimals.Price WHEN NOT MATCHED BY TARGET THEN INSERT (AnimalName, Price) VALUES (FarmAnimals.AnimalName, FarmAnimals.Price) OUTPUT $action, inserted.AnimalName as ins_name, deleted.AnimalName as del_ name, deleted.Price as old_price; --Results: -- $action ins_name del_name old_price -- ------- -------- -------- --------- -- INSERT Sheep NULL NULL -- UPDATE Goat Goat 350.00
|