Integration.MigrateStagedCustomerData
Documentation | WideWorldImportersDW |
Schema | Integration |
Name | MigrateStagedCustomerData |
Script
CREATE PROCEDURE Integration.MigrateStagedCustomerData WITH EXECUTE AS OWNER AS BEGIN SET NOCOUNT ON; SET XACT_ABORT ON; DECLARE @EndOfTime datetime2(7) = '99991231 23:59:59.9999999'; BEGIN TRAN; DECLARE @LineageKey int = (SELECT TOP(1) [Lineage Key] FROM Integration.Lineage WHERE [Table Name] = N'Customer' AND [Data Load Completed] IS NULL ORDER BY [Lineage Key] DESC); WITH RowsToCloseOff AS ( SELECT c.[WWI Customer ID], MIN(c.[Valid From]) AS [Valid From] FROM Integration.Customer_Staging AS c GROUP BY c.[WWI Customer ID] ) UPDATE c SET c.[Valid To] = rtco.[Valid From] FROM Dimension.Customer AS c INNER JOIN RowsToCloseOff AS rtco ON c.[WWI Customer ID] = rtco.[WWI Customer ID] WHERE c.[Valid To] = @EndOfTime; INSERT Dimension.Customer ([WWI Customer ID], Customer, [Bill To Customer], Category, [Buying Group], [Primary Contact], [Postal Code], [Valid From], [Valid To], [Lineage Key]) SELECT [WWI Customer ID], Customer, [Bill To Customer], Category, [Buying Group], [Primary Contact], [Postal Code], [Valid From], [Valid To], @LineageKey FROM Integration.Customer_Staging; UPDATE Integration.Lineage SET [Data Load Completed] = SYSDATETIME(), [Was Successful] = 1 WHERE [Lineage Key] = @LineageKey; UPDATE Integration.[ETL Cutoff] SET [Cutoff Time] = (SELECT [Source System Cutoff Time] FROM Integration.Lineage WHERE [Lineage Key] = @LineageKey) FROM Integration.[ETL Cutoff] WHERE [Table Name] = N'Customer'; COMMIT; RETURN 0; END; |
Exported: 2019-02-04 23:05, Last imported: 2018-08-10 09:59