Integration.GetCustomerUpdates
Documentation | WideWorldImporters |
Schema | Integration |
Name | GetCustomerUpdates |
Input/Output
Mode | Name | Data type | Description | |
---|---|---|---|---|
1 | IN | LastCutoff | datetime2(7) | |
2 | IN | NewCutoff | datetime2(7) |
Script
CREATE PROCEDURE Integration.GetCustomerUpdates @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 #CustomerChanges ( [WWI Customer ID] int, Customer nvarchar(100), [Bill To Customer] nvarchar(100), Category nvarchar(50), [Buying Group] nvarchar(50), [Primary Contact] nvarchar(50), [Postal Code] nvarchar(10), [Valid From] datetime2(7), [Valid To] datetime2(7) NULL ); DECLARE @BuyingGroupID int; DECLARE @CustomerCategoryID int; DECLARE @CustomerID int; DECLARE @ValidFrom datetime2(7); -- first need to find any buying group changes that have occurred since initial load DECLARE BuyingGroupChangeList CURSOR FAST_FORWARD READ_ONLY FOR SELECT bg.BuyingGroupID, bg.ValidFrom FROM Sales.BuyingGroups_Archive AS bg WHERE bg.ValidFrom > @LastCutoff AND bg.ValidFrom <= @NewCutoff AND bg.ValidFrom <> @InitialLoadDate UNION ALL SELECT bg.BuyingGroupID, bg.ValidFrom FROM Sales.BuyingGroups AS bg WHERE bg.ValidFrom > @LastCutoff AND bg.ValidFrom <= @NewCutoff AND bg.ValidFrom <> @InitialLoadDate ORDER BY ValidFrom; OPEN BuyingGroupChangeList; FETCH NEXT FROM BuyingGroupChangeList INTO @BuyingGroupID, @ValidFrom; WHILE @@FETCH_STATUS = 0 BEGIN INSERT #CustomerChanges ([WWI Customer ID], Customer, [Bill To Customer], Category, [Buying Group], [Primary Contact], [Postal Code], [Valid From], [Valid To]) SELECT c.CustomerID, c.CustomerName, bt.CustomerName, cc.CustomerCategoryName, bg.BuyingGroupName, p.FullName, c.DeliveryPostalCode, c.ValidFrom, c.ValidTo FROM Sales.Customers FOR SYSTEM_TIME AS OF @ValidFrom AS c INNER JOIN Sales.BuyingGroups FOR SYSTEM_TIME AS OF @ValidFrom AS bg ON c.BuyingGroupID = bg.BuyingGroupID INNER JOIN Sales.CustomerCategories FOR SYSTEM_TIME AS OF @ValidFrom AS cc ON c.CustomerCategoryID = cc.CustomerCategoryID INNER JOIN Sales.Customers FOR SYSTEM_TIME AS OF @ValidFrom AS bt ON c.BillToCustomerID = bt.CustomerID INNER JOIN [Application].People FOR SYSTEM_TIME AS OF @ValidFrom AS p ON c.PrimaryContactPersonID = p.PersonID WHERE c.BuyingGroupID = @BuyingGroupID; FETCH NEXT FROM BuyingGroupChangeList INTO @BuyingGroupID, @ValidFrom; END; CLOSE BuyingGroupChangeList; DEALLOCATE BuyingGroupChangeList; -- next need to find any customer category changes that have occurred since initial load DECLARE CustomerCategoryChangeList CURSOR FAST_FORWARD READ_ONLY FOR SELECT cc.CustomerCategoryID, cc.ValidFrom FROM Sales.CustomerCategories_Archive AS cc WHERE cc.ValidFrom > @LastCutoff AND cc.ValidFrom <= @NewCutoff AND cc.ValidFrom <> @InitialLoadDate UNION ALL SELECT cc.CustomerCategoryID, cc.ValidFrom FROM Sales.CustomerCategories AS cc WHERE cc.ValidFrom > @LastCutoff AND cc.ValidFrom <= @NewCutoff AND cc.ValidFrom <> @InitialLoadDate ORDER BY ValidFrom; OPEN CustomerCategoryChangeList; FETCH NEXT FROM CustomerCategoryChangeList INTO @CustomerCategoryID, @ValidFrom; WHILE @@FETCH_STATUS = 0 BEGIN INSERT #CustomerChanges ([WWI Customer ID], Customer, [Bill To Customer], Category, [Buying Group], [Primary Contact], [Postal Code], [Valid From], [Valid To]) SELECT c.CustomerID, c.CustomerName, bt.CustomerName, cc.CustomerCategoryName, bg.BuyingGroupName, p.FullName, c.DeliveryPostalCode, c.ValidFrom, c.ValidTo FROM Sales.Customers FOR SYSTEM_TIME AS OF @ValidFrom AS c INNER JOIN Sales.BuyingGroups FOR SYSTEM_TIME AS OF @ValidFrom AS bg ON c.BuyingGroupID = bg.BuyingGroupID INNER JOIN Sales.CustomerCategories FOR SYSTEM_TIME AS OF @ValidFrom AS cc ON c.CustomerCategoryID = cc.CustomerCategoryID INNER JOIN Sales.Customers FOR SYSTEM_TIME AS OF @ValidFrom AS bt ON c.BillToCustomerID = bt.CustomerID INNER JOIN [Application].People FOR SYSTEM_TIME AS OF @ValidFrom AS p ON c.PrimaryContactPersonID = p.PersonID WHERE cc.CustomerCategoryID = @CustomerCategoryID; FETCH NEXT FROM CustomerCategoryChangeList INTO @CustomerCategoryID, @ValidFrom; END; CLOSE CustomerCategoryChangeList; DEALLOCATE CustomerCategoryChangeList; -- finally need to find any customer changes that have occurred, including during the initial load DECLARE CustomerChangeList CURSOR FAST_FORWARD READ_ONLY FOR SELECT c.CustomerID, c.ValidFrom FROM Sales.Customers_Archive AS c WHERE c.ValidFrom > @LastCutoff AND c.ValidFrom <= @NewCutoff UNION ALL SELECT c.CustomerID, c.ValidFrom FROM Sales.Customers AS c WHERE c.ValidFrom > @LastCutoff AND c.ValidFrom <= @NewCutoff ORDER BY ValidFrom; OPEN CustomerChangeList; FETCH NEXT FROM CustomerChangeList INTO @CustomerID, @ValidFrom; WHILE @@FETCH_STATUS = 0 BEGIN INSERT #CustomerChanges ([WWI Customer ID], Customer, [Bill To Customer], Category, [Buying Group], [Primary Contact], [Postal Code], [Valid From], [Valid To]) SELECT c.CustomerID, c.CustomerName, bt.CustomerName, cc.CustomerCategoryName, bg.BuyingGroupName, p.FullName, c.DeliveryPostalCode, c.ValidFrom, c.ValidTo FROM Sales.Customers FOR SYSTEM_TIME AS OF @ValidFrom AS c INNER JOIN Sales.BuyingGroups FOR SYSTEM_TIME AS OF @ValidFrom AS bg ON c.BuyingGroupID = bg.BuyingGroupID INNER JOIN Sales.CustomerCategories FOR SYSTEM_TIME AS OF @ValidFrom AS cc ON c.CustomerCategoryID = cc.CustomerCategoryID INNER JOIN Sales.Customers FOR SYSTEM_TIME AS OF @ValidFrom AS bt ON c.BillToCustomerID = bt.CustomerID INNER JOIN [Application].People FOR SYSTEM_TIME AS OF @ValidFrom AS p ON c.PrimaryContactPersonID = p.PersonID WHERE c.CustomerID = @CustomerID; FETCH NEXT FROM CustomerChangeList INTO @CustomerID, @ValidFrom; END; CLOSE CustomerChangeList; DEALLOCATE CustomerChangeList; -- add an index to make lookups faster CREATE INDEX IX_CustomerChanges ON #CustomerChanges ([WWI Customer ID], [Valid From]); -- work out the [Valid To] value by taking the [Valid From] of any row that's for the same customer but later -- otherwise take the end of time UPDATE cc SET [Valid To] = COALESCE((SELECT MIN([Valid From]) FROM #CustomerChanges AS cc2 WHERE cc2.[WWI Customer ID] = cc.[WWI Customer ID] AND cc2.[Valid From] > cc.[Valid From]), @EndOfTime) FROM #CustomerChanges AS cc; SELECT [WWI Customer ID], Customer, [Bill To Customer], Category, [Buying Group], [Primary Contact], [Postal Code], [Valid From], [Valid To] FROM #CustomerChanges ORDER BY [Valid From]; DROP TABLE #CustomerChanges; RETURN 0; END; |
Exported: 2019-02-04 23:04, Last imported: 2018-08-10 09:51