Integration.GetSupplierUpdates
Documentation | WideWorldImporters |
Schema | Integration |
Name | GetSupplierUpdates |
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.GetSupplierUpdates @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'; DECLARE @InitialLoadDate date = '20130101'; CREATE TABLE #SupplierChanges ( [WWI Supplier ID] int, Supplier nvarchar(100), Category nvarchar(50), [Primary Contact] nvarchar(50), [Supplier Reference] nvarchar(20), [Payment Days] int, [Postal Code] nvarchar(10), [Valid From] datetime2(7), [Valid To] datetime2(7) ); DECLARE @SupplierCategoryID int; DECLARE @SupplierID int; DECLARE @ValidFrom datetime2(7); -- need to find any Supplier category changes that have occurred since initial load DECLARE SupplierCategoryChangeList CURSOR FAST_FORWARD READ_ONLY FOR SELECT cc.SupplierCategoryID, cc.ValidFrom FROM Purchasing.SupplierCategories_Archive AS cc WHERE cc.ValidFrom > @LastCutoff AND cc.ValidFrom <= @NewCutoff AND cc.ValidFrom <> @InitialLoadDate UNION ALL SELECT cc.SupplierCategoryID, cc.ValidFrom FROM Purchasing.SupplierCategories AS cc WHERE cc.ValidFrom > @LastCutoff AND cc.ValidFrom <= @NewCutoff AND cc.ValidFrom <> @InitialLoadDate ORDER BY ValidFrom; OPEN SupplierCategoryChangeList; FETCH NEXT FROM SupplierCategoryChangeList INTO @SupplierCategoryID, @ValidFrom; WHILE @@FETCH_STATUS = 0 BEGIN INSERT #SupplierChanges ([WWI Supplier ID], Supplier, Category, [Primary Contact], [Supplier Reference], [Payment Days], [Postal Code], [Valid From], [Valid To]) SELECT s.SupplierID, s.SupplierName, sc.SupplierCategoryName, p.FullName, s.SupplierReference, s.PaymentDays, s.DeliveryPostalCode, s.ValidFrom, s.ValidTo FROM Purchasing.Suppliers FOR SYSTEM_TIME AS OF @ValidFrom AS s INNER JOIN Purchasing.SupplierCategories FOR SYSTEM_TIME AS OF @ValidFrom AS sc ON s.SupplierCategoryID = sc.SupplierCategoryID INNER JOIN [Application].People FOR SYSTEM_TIME AS OF @ValidFrom AS p ON s.PrimaryContactPersonID = p.PersonID WHERE sc.SupplierCategoryID = @SupplierCategoryID; FETCH NEXT FROM SupplierCategoryChangeList INTO @SupplierCategoryID, @ValidFrom; END; CLOSE SupplierCategoryChangeList; DEALLOCATE SupplierCategoryChangeList; -- finally need to find any Supplier changes that have occurred, including during the initial load DECLARE SupplierChangeList CURSOR FAST_FORWARD READ_ONLY FOR SELECT c.SupplierID, c.ValidFrom FROM Purchasing.Suppliers_Archive AS c WHERE c.ValidFrom > @LastCutoff AND c.ValidFrom <= @NewCutoff UNION ALL SELECT c.SupplierID, c.ValidFrom FROM Purchasing.Suppliers AS c WHERE c.ValidFrom > @LastCutoff AND c.ValidFrom <= @NewCutoff ORDER BY ValidFrom; OPEN SupplierChangeList; FETCH NEXT FROM SupplierChangeList INTO @SupplierID, @ValidFrom; WHILE @@FETCH_STATUS = 0 BEGIN INSERT #SupplierChanges ([WWI Supplier ID], Supplier, Category, [Primary Contact], [Supplier Reference], [Payment Days], [Postal Code], [Valid From], [Valid To]) SELECT s.SupplierID, s.SupplierName, sc.SupplierCategoryName, p.FullName, s.SupplierReference, s.PaymentDays, s.DeliveryPostalCode, s.ValidFrom, s.ValidTo FROM Purchasing.Suppliers FOR SYSTEM_TIME AS OF @ValidFrom AS s INNER JOIN Purchasing.SupplierCategories FOR SYSTEM_TIME AS OF @ValidFrom AS sc ON s.SupplierCategoryID = sc.SupplierCategoryID INNER JOIN [Application].People FOR SYSTEM_TIME AS OF @ValidFrom AS p ON s.PrimaryContactPersonID = p.PersonID WHERE s.SupplierID = @SupplierID; FETCH NEXT FROM SupplierChangeList INTO @SupplierID, @ValidFrom; END; CLOSE SupplierChangeList; DEALLOCATE SupplierChangeList; -- add an index to make lookups faster CREATE INDEX IX_SupplierChanges ON #SupplierChanges ([WWI Supplier ID], [Valid From]); -- work out the [Valid To] value by taking the [Valid From] of any row that's for the same Supplier but later -- otherwise take the end of time UPDATE cc SET [Valid To] = COALESCE((SELECT MIN([Valid From]) FROM #SupplierChanges AS cc2 WHERE cc2.[WWI Supplier ID] = cc.[WWI Supplier ID] AND cc2.[Valid From] > cc.[Valid From]), @EndOfTime) FROM #SupplierChanges AS cc; SELECT [WWI Supplier ID], Supplier, Category, [Primary Contact], [Supplier Reference], [Payment Days], [Postal Code], [Valid From], [Valid To] FROM #SupplierChanges ORDER BY [Valid From]; DROP TABLE #SupplierChanges; RETURN 0; END; |
Exported: 2019-02-04 23:04, Last imported: 2018-08-10 09:51