SQL Server 2008的實用小道具——merger
根據(jù)在另一個表中找到的差異在一個表中插入、更新或刪除行,可以對兩個表進行同步。
A. 使用 MERGE 在單個語句中對表執(zhí)行 UPDATE 和 DELETE 操作
下面的示例使用 MERGE 根據(jù) SalesOrderDetail 表中已處理的訂單,每天更新 AdventureWorks 示例數(shù)據(jù)庫中的 ProductInventory 表。通過減去每天對 SalesOrderDetail 表中的每種產(chǎn)品所下的訂單數(shù),更新 ProductInventory 表的 Quantity 列。如果某種產(chǎn)品的訂單數(shù)導(dǎo)致該產(chǎn)品的庫存量下降到 0 或更少,則會從 ProductInventory 表中刪除該產(chǎn)品對應(yīng)的行。
- USE AdventureWorks;
- GO
- IF OBJECT_ID (N'Production.usp_UpdateInventory', N'P')
- IS NOT NULL DROP PROCEDURE Production.usp_UpdateInventory;
- GO
- CREATE PROCEDURE Production.usp_UpdateInventory
- @OrderDate datetime
- AS
- MERGE Production.ProductInventory AS target
- USING (SELECT ProductID, SUM(OrderQty) FROM Sales.SalesOrderDetail AS sod
- JOIN Sales.SalesOrderHeader AS soh
- ON sod.SalesOrderID = soh.SalesOrderID
- AND soh.OrderDate = @OrderDate
- GROUP BY ProductID) AS source (ProductID, OrderQty)
- ON (target.ProductID = source.ProductID)
- WHEN MATCHED AND target.Quantity - source.OrderQty <= 0
- THEN DELETE
- WHEN MATCHED
- THEN UPDATE SET target.Quantity = target.Quantity - source.OrderQty,
- target.ModifiedDate = GETDATE()
- OUTPUT $action, Inserted.ProductID, Inserted.Quantity, Inserted.ModifiedDate, Deleted.ProductID,
- Deleted.Quantity, Deleted.ModifiedDate;
- GO
- EXECUTE Production.usp_UpdateInventory '20030501'
B. 借助派生的源表,使用 MERGE 對目標(biāo)表執(zhí)行 UPDATE 和 INSERT 操作
下面的示例使用 MERGE 以更新或插入行的方式來修改 SalesReason 表。當(dāng)源表中的 NewName 值與目標(biāo)表 (SalesReason) 的 Name 列中的值匹配時,就會更新此目標(biāo)表中的 ReasonType 列。當(dāng) NewName 的值不匹配時,就會將源行插入到目標(biāo)表中。此源表是一個派生表,它使用 Transact-SQL 行構(gòu)造函數(shù)功能指定源表的多個行。有關(guān)在派生表中使用行構(gòu)造函數(shù)的詳細信息,請參閱 FROM (Transact-SQL)。
- USE AdventureWorks;
- GO
- 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, inserted.*, deleted.*;
C. 將 MERGE 語句的執(zhí)行結(jié)果插入到另一個表中
下例捕獲從 MERGE 語句的 OUTPUT 子句返回的數(shù)據(jù),并將該數(shù)據(jù)插入另一個表。MERGE 語句根據(jù)在 SalesOrderDetail 表中處理的訂單,更新 ProductInventory 表的 Quantity 列。本示例捕獲已更新的行,并將這些行插入用于跟蹤庫存變化的另一個表中。
- USE AdventureWorks;
- GO
- CREATE TABLE Production.UpdatedInventory
- (ProductID INT NOT NULL, LocationID int, NewQty int, PreviousQty int,
- CONSTRAINT PK_Inventory PRIMARY KEY CLUSTERED (ProductID, LocationID));
- GO
- INSERT INTO Production.UpdatedInventory
- SELECT ProductID, LocationID, NewQty, PreviousQty
- FROM
- ( MERGE Production.ProductInventory AS pi
- USING (SELECT ProductID, SUM(OrderQty)
- FROM Sales.SalesOrderDetail AS sod
- JOIN Sales.SalesOrderHeader AS soh
- ON sod.SalesOrderID = soh.SalesOrderID
- AND soh.OrderDate BETWEEN '20030701' AND '20030731'
- GROUP BY ProductID) AS src (ProductID, OrderQty)
- ON pi.ProductID = src.ProductID
- WHEN MATCHED AND pi.Quantity - src.OrderQty >= 0
- THEN UPDATE SET pi.Quantity = pi.Quantity - src.OrderQty
- WHEN MATCHED AND pi.Quantity - src.OrderQty <= 0
- THEN DELETE
- OUTPUT $action, Inserted.ProductID, Inserted.LocationID, Inserted.Quantity AS NewQty, Deleted.Quantity AS PreviousQty)
- AS Changes (Action, ProductID, LocationID, NewQty, PreviousQty) WHERE Action = 'UPDATE';
- GO
原文標(biāo)題:SQL SERVER 2008的幾個新東西:插入,刪除,修改一起來(適合數(shù)據(jù)的同步)-----merger
鏈接: http://www.cnblogs.com/buaaboyi/archive/2010/09/05/1818281.html
【編輯推薦】