Integration.GetCityUpdates
Documentation | WideWorldImporters |
Schema | Integration |
Name | GetCityUpdates |
Input/Output
Mode | Name | Data type | Description | |
---|---|---|---|---|
1 | IN | LastCutoff | datetime2(7) | |
2 | IN | NewCutoff | datetime2(7) |
Script
CREATE PROCEDURE Integration.GetCityUpdates @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 #CityChanges ( [WWI City ID] int, City nvarchar(50), [State Province] nvarchar(50), Country nvarchar(50), Continent nvarchar(30), [Sales Territory] nvarchar(50), Region nvarchar(30), Subregion nvarchar(30), [Location] geography, [Latest Recorded Population] bigint, [Valid From] datetime2(7), [Valid To] datetime2(7) NULL ); DECLARE @CountryID int; DECLARE @StateProvinceID int; DECLARE @CityID int; DECLARE @ValidFrom datetime2(7); -- first need to find any country changes that have occurred since initial load DECLARE CountryChangeList CURSOR FAST_FORWARD READ_ONLY FOR SELECT co.CountryID, co.ValidFrom FROM [Application].Countries_Archive AS co WHERE co.ValidFrom > @LastCutoff AND co.ValidFrom <= @NewCutoff AND co.ValidFrom <> @InitialLoadDate UNION ALL SELECT co.CountryID, co.ValidFrom FROM [Application].Countries AS co WHERE co.ValidFrom > @LastCutoff AND co.ValidFrom <= @NewCutoff AND co.ValidFrom <> @InitialLoadDate ORDER BY ValidFrom; OPEN CountryChangeList; FETCH NEXT FROM CountryChangeList INTO @CountryID, @ValidFrom; WHILE @@FETCH_STATUS = 0 BEGIN INSERT #CityChanges ([WWI City ID], City, [State Province], Country, Continent, [Sales Territory], Region, Subregion, [Location], [Latest Recorded Population], [Valid From], [Valid To]) SELECT c.CityID, c.CityName, sp.StateProvinceName, co.CountryName, co.Continent, sp.SalesTerritory, co.Region, co.Subregion, c.[Location], COALESCE(c.LatestRecordedPopulation, 0), @ValidFrom, NULL FROM [Application].Cities FOR SYSTEM_TIME AS OF @ValidFrom AS c INNER JOIN [Application].StateProvinces FOR SYSTEM_TIME AS OF @ValidFrom AS sp ON c.StateProvinceID = sp.StateProvinceID INNER JOIN [Application].Countries FOR SYSTEM_TIME AS OF @ValidFrom AS co ON sp.CountryID = co.CountryID WHERE co.CountryID = @CountryID; FETCH NEXT FROM CountryChangeList INTO @CountryID, @ValidFrom; END; CLOSE CountryChangeList; DEALLOCATE CountryChangeList; -- next need to find any stateprovince changes that have occurred since initial load DECLARE StateProvinceChangeList CURSOR FAST_FORWARD READ_ONLY FOR SELECT sp.StateProvinceID, sp.ValidFrom FROM [Application].StateProvinces_Archive AS sp WHERE sp.ValidFrom > @LastCutoff AND sp.ValidFrom <= @NewCutoff AND sp.ValidFrom <> @InitialLoadDate UNION ALL SELECT sp.StateProvinceID, sp.ValidFrom FROM [Application].StateProvinces AS sp WHERE sp.ValidFrom > @LastCutoff AND sp.ValidFrom <= @NewCutoff AND sp.ValidFrom <> @InitialLoadDate ORDER BY ValidFrom; OPEN StateProvinceChangeList; FETCH NEXT FROM StateProvinceChangeList INTO @StateProvinceID, @ValidFrom; WHILE @@FETCH_STATUS = 0 BEGIN INSERT #CityChanges ([WWI City ID], City, [State Province], Country, Continent, [Sales Territory], Region, Subregion, [Location], [Latest Recorded Population], [Valid From], [Valid To]) SELECT c.CityID, c.CityName, sp.StateProvinceName, co.CountryName, co.Continent, sp.SalesTerritory, co.Region, co.Subregion, c.[Location], COALESCE(c.LatestRecordedPopulation, 0), @ValidFrom, NULL FROM [Application].Cities FOR SYSTEM_TIME AS OF @ValidFrom AS c INNER JOIN [Application].StateProvinces FOR SYSTEM_TIME AS OF @ValidFrom AS sp ON c.StateProvinceID = sp.StateProvinceID INNER JOIN [Application].Countries FOR SYSTEM_TIME AS OF @ValidFrom AS co ON sp.CountryID = co.CountryID WHERE sp.StateProvinceID = @StateProvinceID; FETCH NEXT FROM StateProvinceChangeList INTO @StateProvinceID, @ValidFrom; END; CLOSE StateProvinceChangeList; DEALLOCATE StateProvinceChangeList; -- finally need to find any city changes that have occurred, including during the initial load DECLARE CityChangeList CURSOR FAST_FORWARD READ_ONLY FOR SELECT c.CityID, c.ValidFrom FROM [Application].Cities_Archive AS c WHERE c.ValidFrom > @LastCutoff AND c.ValidFrom <= @NewCutoff UNION ALL SELECT c.CityID, c.ValidFrom FROM [Application].Cities AS c WHERE c.ValidFrom > @LastCutoff AND c.ValidFrom <= @NewCutoff ORDER BY ValidFrom; OPEN CityChangeList; FETCH NEXT FROM CityChangeList INTO @CityID, @ValidFrom; WHILE @@FETCH_STATUS = 0 BEGIN INSERT #CityChanges ([WWI City ID], City, [State Province], Country, Continent, [Sales Territory], Region, Subregion, [Location], [Latest Recorded Population], [Valid From], [Valid To]) SELECT c.CityID, c.CityName, sp.StateProvinceName, co.CountryName, co.Continent, sp.SalesTerritory, co.Region, co.Subregion, c.[Location], COALESCE(c.LatestRecordedPopulation, 0), @ValidFrom, NULL FROM [Application].Cities FOR SYSTEM_TIME AS OF @ValidFrom AS c INNER JOIN [Application].StateProvinces FOR SYSTEM_TIME AS OF @ValidFrom AS sp ON c.StateProvinceID = sp.StateProvinceID INNER JOIN [Application].Countries FOR SYSTEM_TIME AS OF @ValidFrom AS co ON sp.CountryID = co.CountryID WHERE c.CityID = @CityID; FETCH NEXT FROM CityChangeList INTO @CityID, @ValidFrom; END; CLOSE CityChangeList; DEALLOCATE CityChangeList; -- add an index to make lookups faster CREATE INDEX IX_CityChanges ON #CityChanges ([WWI City ID], [Valid From]); -- work out the [Valid To] value by taking the [Valid From] of any row that's for the same city but later -- otherwise take the end of time UPDATE cc SET [Valid To] = COALESCE((SELECT MIN([Valid From]) FROM #CityChanges AS cc2 WHERE cc2.[WWI City ID] = cc.[WWI City ID] AND cc2.[Valid From] > cc.[Valid From]), @EndOfTime) FROM #CityChanges AS cc; SELECT [WWI City ID], City, [State Province], Country, Continent, [Sales Territory], Region, Subregion, [Location] geography, [Latest Recorded Population], [Valid From], [Valid To] FROM #CityChanges ORDER BY [Valid From]; DROP TABLE #CityChanges; RETURN 0; END; |
Exported: 2019-02-04 23:04, Last imported: 2018-08-10 09:51