Integration.GetStockItemUpdates
Documentation | WideWorldImporters |
Schema | Integration |
Name | GetStockItemUpdates |
Helper procedures for the ETL package
Input/Output
Mode | Name | Data type | Description | |
---|---|---|---|---|
1 | IN | LastCutoff | datetime2(7) | |
2 | IN | NewCutoff | datetime2(7) |
Script
CREATE PROCEDURE Integration.GetStockItemUpdates @LastCutoff datetime2(7), @NewCutoff datetime2(7) WITH EXECUTE AS OWNER AS BEGIN SET NOCOUNT ON; SET XACT_ABORT ON; DECLARE @EndOfTime datetime2(7) = '99991231 23:59:59.9999999'; CREATE TABLE #StockItemChanges ( [WWI Stock Item ID] int, [Stock Item] nvarchar(100), Color nvarchar(20), [Selling Package] nvarchar(50), [Buying Package] nvarchar(50), Brand nvarchar(50), Size nvarchar(20), [Lead Time Days] int, [Quantity Per Outer] int, [Is Chiller Stock] bit, Barcode nvarchar(50), [Tax Rate] decimal(18,3), [Unit Price] decimal(18,2), [Recommended Retail Price] decimal(18,2), [Typical Weight Per Unit] decimal(18,3), Photo varbinary(max), [Valid From] datetime2(7), [Valid To] datetime2(7) ); DECLARE @StockItemID int; DECLARE @ValidFrom datetime2(7); -- need to find any StockItem changes that have occurred, including during the initial load DECLARE StockItemChangeList CURSOR FAST_FORWARD READ_ONLY FOR SELECT c.StockItemID, c.ValidFrom FROM Warehouse.StockItems_Archive AS c WHERE c.ValidFrom > @LastCutoff AND c.ValidFrom <= @NewCutoff UNION ALL SELECT c.StockItemID, c.ValidFrom FROM Warehouse.StockItems AS c WHERE c.ValidFrom > @LastCutoff AND c.ValidFrom <= @NewCutoff ORDER BY ValidFrom; OPEN StockItemChangeList; FETCH NEXT FROM StockItemChangeList INTO @StockItemID, @ValidFrom; WHILE @@FETCH_STATUS = 0 BEGIN INSERT #StockItemChanges ([WWI Stock Item ID], [Stock Item], Color, [Selling Package], [Buying Package], Brand, Size, [Lead Time Days], [Quantity Per Outer], [Is Chiller Stock], Barcode, [Tax Rate], [Unit Price], [Recommended Retail Price], [Typical Weight Per Unit], Photo, [Valid From], [Valid To]) SELECT si.StockItemID, si.StockItemName, c.ColorName, spt.PackageTypeName, bpt.PackageTypeName, si.Brand, si.Size, si.LeadTimeDays, si.QuantityPerOuter, si.IsChillerStock, si.Barcode, si.LeadTimeDays, si.UnitPrice, si.RecommendedRetailPrice, si.TypicalWeightPerUnit, si.Photo, si.ValidFrom, si.ValidTo FROM Warehouse.StockItems FOR SYSTEM_TIME AS OF @ValidFrom AS si INNER JOIN Warehouse.PackageTypes FOR SYSTEM_TIME AS OF @ValidFrom AS spt ON si.UnitPackageID = spt.PackageTypeID INNER JOIN Warehouse.PackageTypes FOR SYSTEM_TIME AS OF @ValidFrom AS bpt ON si.OuterPackageID = bpt.PackageTypeID LEFT OUTER JOIN Warehouse.Colors FOR SYSTEM_TIME AS OF @ValidFrom AS c ON si.ColorID = c.ColorID WHERE si.StockItemID = @StockItemID; FETCH NEXT FROM StockItemChangeList INTO @StockItemID, @ValidFrom; END; CLOSE StockItemChangeList; DEALLOCATE StockItemChangeList; -- add an index to make lookups faster CREATE INDEX IX_StockItemChanges ON #StockItemChanges ([WWI Stock Item ID], [Valid From]); -- work out the [Valid To] value by taking the [Valid From] of any row that's for the same StockItem but later -- otherwise take the end of time UPDATE cc SET [Valid To] = COALESCE((SELECT MIN([Valid From]) FROM #StockItemChanges AS cc2 WHERE cc2.[WWI Stock Item ID] = cc.[WWI Stock Item ID] AND cc2.[Valid From] > cc.[Valid From]), @EndOfTime) FROM #StockItemChanges AS cc; SELECT [WWI Stock Item ID], [Stock Item], ISNULL(Color, N'N/A') AS Color, [Selling Package], [Buying Package], ISNULL(Brand, N'N/A') AS Brand, ISNULL(Size, N'N/A') AS Size, [Lead Time Days], [Quantity Per Outer], [Is Chiller Stock], ISNULL(Barcode, N'N/A') AS Barcode, [Tax Rate], [Unit Price], [Recommended Retail Price], [Typical Weight Per Unit], Photo, [Valid From], [Valid To] FROM #StockItemChanges ORDER BY [Valid From]; DROP TABLE #StockItemChanges; RETURN 0; END; |
Exported: 2019-02-04 23:04, Last imported: 2018-08-10 09:51