USE Arizona
GO

BEGIN TRANSACTION 
SET XACT_ABORT ON;
SET NOCOUNT ON;

--revert address changes
UPDATE [ad]
SET [ad].[AD_first_name]             = c.AD_first_name,
    [ad].[AD_last_name]              = C.AD_last_name,
    [ad].[AD_middle_name]            = c.AD_middle_name,
    [ad].[AD_name]                   = c.AD_name,
    [ad].[AD_title]                  = c.AD_title,
    [ad].[AD_sex]                    = c.AD_sex,
    [ad].[AD_address_1]              = c.AD_address_1,
    [ad].[AD_address_2]              = c.AD_address_2,
    [ad].[AD_address_3]              = c.AD_address_3,
    [ad].[AD_address_supplement]     = c.AD_address_supplement,
    [ad].[AD_zip_code]               = c.AD_zip_code,
    [ad].[AD_city]                   = c.AD_city,
    [ad].[AD_state]                  = c.AD_state,
    [ad].[AD_postal_routing_address] = c.AD_postal_routing_address,
    [ad].[AD_language]               = c.AD_language,
    [ad].[AD_country]                = c.AD_country
FROM dbo.Address ad
  JOIN HCITools.tmp.tst_sp_correction c ON c.Address_ID = ad.Address_ID
WHERE ad.AD_name <> c.AD_name;


--revert address_key changes
UPDATE adk
  SET adk.ADK_key = c.ADK_key
FROM dbo.Address_key adk
  JOIN HCITools.tmp.tst_sp_correction c ON c.Address_ID = adk.ADK_address
WHERE adk.ADK_type=5;

ROLLBACK TRANSACTION 
--COMMIT TRANSACTION 
