程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> SqlServer數據庫 >> SqlServer2008 >> SQLServer2008的實用小道具 merger使用介紹

SQLServer2008的實用小道具 merger使用介紹

編輯:SqlServer2008
A. 使用 MERGE 在單個語句中對表執行 UPDATE 和 DELETE 操作
  下面的示例使用 MERGE 根據 SalesOrderDetail 表中已處理的訂單,每天更新 AdventureWorks 示例數據庫中的 ProductInventory 表。通過減去每天對 SalesOrderDetail 表中的每種產品所下的訂單數,更新 ProductInventory 表的 Quantity 列。如果某種產品的訂單數導致該產品的庫存量下降到 0 或更少,則會從 ProductInventory 表中刪除該產品對應的行。
  B. 借助派生的源表,使用 MERGE 對目標表執行 UPDATE 和 INSERT 操作
  下面的示例使用 MERGE 以更新或插入行的方式來修改 SalesReason 表。當源表中的 NewName 值與目標表 (SalesReason) 的 Name 列中的值匹配時,就會更新此目標表中的 ReasonType 列。當 NewName 的值不匹配時,就會將源行插入到目標表中。此源表是一個派生表,它使用 Transact-SQL 行構造函數功能指定源表的多個行。有關在派生表中使用行構造函數的詳細信息,請參閱 FROM (Transact-SQL)。

  C. 將 MERGE 語句的執行結果插入到另一個表中
  下例捕獲從 MERGE 語句的 OUTPUT 子句返回的數據,並將該數據插入另一個表。MERGE 語句根據在 SalesOrderDetail 表中處理的訂單,更新 ProductInventory 表的 Quantity 列。本示例捕獲已更新的行,並將這些行插入用於跟蹤庫存變化的另一個表中
復制代碼 代碼如下:
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'

復制代碼 代碼如下:
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.*;

復制代碼 代碼如下:
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.*;

復制代碼 代碼如下:
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
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved