SELECT Time_class, Time_instance, Geo_class, Aggregated_area, Super_classed_disease_cat, Cause, Constraint_ref, Sex, Sex_code, Age_start, Age_end, Age_range, Sum_count = SUM(G.Converted_person_count) INTO #tmp_G_P_M_CC_S_S FROM Geo_postaggregated_mortality_count_with_cause_catagory_SPLIT_SMR G GROUP BY G.Time_class, G.Time_instance, G.Geo_class, G.Aggregated_area, G.Super_classed_disease_cat, G.Cause, G.Constraint_ref, G.Sex, G.Sex_code, G.Age_start, G.Age_end, G.Age_range DECLARE @Key AS INT SET @Key = 1 while @Key <= (SELECT MAX(Key_m) FROM Geo_postaggregated_mortality_count_with_cause_catagory_SPLIT_SMR) begin UPDATE Geo_postaggregated_pop_count_SPLIT SET Total_person_count = t.Sum_count FROM Geo_postaggregated_mortality_count_with_cause_catagory_SPLIT_SMR G, #tmp_G_P_M_CC_S_S t WHERE G.Key_m = @Key, --- Problem is here G.Time_instance = t.Time_instance, G.Geo_class = t.Geo_class, G.Aggregated_area = t.Aggregated_area, G.Super_classed_disease_cat = t.Super_classed_disease_cat, G.Cause = t.Cause, G.Constraint_ref = t.Constraint_ref, G.Sex = t.Sex, G.Sex_code = t.Sex_code, G.Age_start = t.Age_start, G.Age_end = t.Age_end, G.Age_range = t.Age_range SET @Key = @Key +1 end