You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
USE AdventureWorks2012;
GO
IF OBJECT_ID(N'Production.ZeroInventory', N'U') IS NOT NULLDROPTABLEProduction.ZeroInventory;
GO
--Create ZeroInventory table. CREATETABLEProduction.ZeroInventory (DeletedProductID int, RemovedOnDate DateTime);
GO
INSERT INTOProduction.ZeroInventory (DeletedProductID, RemovedOnDate)
SELECT ProductID, GETDATE()
FROM
( MERGE Production.ProductInventoryAS pi
USING (SELECT ProductID, SUM(OrderQty) FROMSales.SalesOrderDetailAS sod
JOINSales.SalesOrderHeaderAS soh
ONsod.SalesOrderID=soh.SalesOrderIDANDsoh.OrderDate='20070401'GROUP BY ProductID) AS src (ProductID, OrderQty)
ON (pi.ProductID=src.ProductID)
WHEN MATCHED ANDpi.Quantity-src.OrderQty<=0
THEN DELETE
WHEN MATCHED
THEN UPDATESETpi.Quantity=pi.Quantity-src.OrderQty
OUTPUT $action, deleted.ProductID) AS Changes (Action, ProductID)
WHERE Action ='DELETE';
IF @@ROWCOUNT =0
PRINT 'Warning: No rows were inserted';
GO
SELECT DeletedProductID, RemovedOnDate
FROMProduction.ZeroInventory;
GO
-- Reference/Transact-SQL (T-SQL) Reference/Queries/General/OUTPUT Clause-- https://docs.microsoft.com/en-us/sql/t-sql/queries/output-clause-transact-sql?view=sql-server-ver15
input
insert into his
select action,id from
(
merge tab1 using (select id,name from tab2) as v1
on (tab1.id=v1.id)
when matched then updatesettab1.name=v1.name
output $action,deleted.id
) as v2(action,name)
4109
The text was updated successfully, but these errors were encountered:
4109
The text was updated successfully, but these errors were encountered: