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

SQLServer2008的適用大道具 merger應用引見

編輯:MSSQL2008

SQLServer2008的適用大道具 merger應用引見。本站提示廣大學習愛好者:(SQLServer2008的適用大道具 merger應用引見)文章只能為提供參考,不一定能成為您想要的結果。以下是SQLServer2008的適用大道具 merger應用引見正文


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