1 use AdventureWorks
2 go
3 createprocedure dbo.InsertUnitMeasure @UnitMeasureCodenchar(3),@Namenvarchar(25)
4 as
5 begin
6 set nocount on;
7 update Production.UnitMeasure set Name=@Namewhere UnitMeasureCode=@UnitMeasureCode
8 if(@@ROWCOUNT=0)
9 begin
10 insertinto Production.UnitMeasure(Name,UnitMeasureCode)values(@Name,@UnitMeasureCode)
11 end
12 end
13 go
1 alterprocedure dbo.InsertUnitMeasure @UnitMeasureCodenchar(3),@Namenvarchar(25)
2 as
3 begin
4 set nocount on
5 merge Production.UnitMeasure as target
6 using (select@UnitMeasureCode,@Name) as source (UnitMeasureCode,Name)
7 on (target.UnitMeasureCode=source.UnitMeasureCode)
8 when matched thenupdateset Name=source.Name
9 whennot matched theninsert(UnitMeasureCode,Name)values(source.UnitMeasureCode,Name)
10 output deleted.*,$action,inserted.*into MyTempTable;
11 end
12 go
1 createtable MyTempTable(
2 ExistingCode nchar(3),
3 ExistingName nvarchar(50),
4 ExistingDate datetime,
5 ActionTaken nvarchar(50),
6 NewCode nchar(3),
7 [NewName]nvarchar(50),
8 NewDate datetime
9 )
10 Go
1 exec InsertUnitMeasure @UnitMeasureCode='ABC',@Name='New Test Value1'
2 EXEC InsertUnitMeasure @UnitMeasureCode='XYZ', @Name='Test Value';
3 EXEC InsertUnitMeasure @UnitMeasureCode='ABC', @Name='Another Test Valuea';
4 Go
1 CREATEPROCEDURE Production.usp_UpdateInventory
2 @OrderDatedatetime
3 AS
4 MERGE Production.ProductInventory AS target
5 USING (SELECT ProductID, SUM(OrderQty) FROM Sales.SalesOrderDetail AS sod
6 JOIN Sales.SalesOrderHeader AS soh
7 ON sod.SalesOrderID = soh.SalesOrderID
8 AND soh.OrderDate =@OrderDate
9 GROUPBY ProductID) AS source (ProductID, OrderQty)
10 ON (target.ProductID = source.ProductID)
11 WHEN MATCHED AND target.Quantity - source.OrderQty <=0
12 THENDELETE
13 WHEN MATCHED
14 THENUPDATESET target.Quantity = target.Quantity - source.OrderQty,
15 target.ModifiedDate =GETDATE()
16 OUTPUT $action, Inserted.ProductID, Inserted.Quantity, Inserted.ModifiedDate, Deleted.ProductID,
17 Deleted.Quantity, Deleted.ModifiedDate;
18 GO
1 declare@SummaryOfChangestable(Change varchar(20))
2 merge into Sales.SalesReason as target
3 using(values('Recommendation','Other'),('Review','Marketing'),('Internet','Promotion')) as source([NewName],NewReasonType)
4 on target.Name=source.[NewName]
5 when matched thenupdateset ReasonType=source.NewReasonType
6 whennot matched by target theninsert(Name,ReasonType) values ([NewName],NewReasonType)
7 output $action into@SummaryOfChanges;
8 select Change,COUNT(*) as CountPerChange from@SummaryOfChangesgroupby Change
1 INSERTINTO Production.UpdatedInventory
2 SELECT ProductID, LocationID, NewQty, PreviousQty
3 FROM
4 ( MERGE Production.ProductInventory AS target
5 USING (SELECT ProductID, SUM(OrderQty)
6 FROM Sales.SalesOrderDetail AS sod
7 JOIN Sales.SalesOrderHeader AS soh
8 ON sod.SalesOrderID = soh.SalesOrderID
9 AND soh.OrderDate BETWEEN'20030701'AND'20030731'
10 GROUPBY ProductID) AS source (ProductID, OrderQty)
11 ON target.ProductID = source.ProductID
12 WHEN MATCHED AND target.Quantity - source.OrderQty >=0
13 THENUPDATESET target.Quantity = target.Quantity - source.OrderQty
14 WHEN MATCHED AND target.Quantity - source.OrderQty <=0
15 THENDELETE
16 OUTPUT $action, Inserted.ProductID, Inserted.LocationID, Inserted.Quantity AS NewQty, Deleted.Quantity AS PreviousQty)
17 AS Changes (Action, ProductID, LocationID, NewQty, PreviousQty) WHERE Action ='UPDATE';
18 GO
