SQL Server 2008的实用小道具——merger[MSSQL防范]
本文“SQL Server 2008的实用小道具——merger[MSSQL防范]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:
按照在另一个表中找到的差别在一个表中插入、更新或删除行,可以对两个表举行同步.
A. 利用 MERGE 在单个语句中对表履行 UPDATE 和 DELETE 操作
下面的示例利用 MERGE 按照 SalesOrderDetail 表中已处理的订单,每天更新 AdventureWorks 示例数据库中的 ProductInventory 表.通过减去每天对 SalesOrderDetail 表中的每种产品所下的订单数,更新 ProductInventory 表的 Quantity 列.假如某种产品的订单数招致该产品的库存量下降到 0 或更少,则会从 ProductInventory 表中删除该产品对应的行.
- 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 对目标表履行 UPDATE 和 INSERT 操作
下面的示例利用 MERGE 以更新或插入行的方法来改正 SalesReason 表.当源表中的 NewName 值与目标表 (SalesReason) 的 Name 列中的值匹配时,就会更新此目标表中的 ReasonType 列.当 NewName 的值不匹配时,就会将源行插入到目标表中.此源表是一个派生表,它利用 Transact-SQL 行构造函数功效指定源表的多个行.有关在派生表中利用行构造函数的具体信息,请参阅 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 语句的履行后果插入到另一个表中
下例捕捉从 MERGE 语句的 OUTPUT 子句返回的数据,并将该数据插入另一个表.MERGE 语句按照在 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
原文标题:SQL SERVER 2008的几个新东西:插入,删除,改正一同来(合适数据的同步)-----merger
链接: http://www.cnblogs.com/buaaboyi/archive/2010/09/05/1818281.html
<以上是“SQL Server 2008的实用小道具——merger[MSSQL防范]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
本文地址: | 与您的QQ/BBS好友分享! |