Print 'Doing pre run Checks to ensure script not already run'; Select * from Script_History where Script_Name='PhonesActive221224'; if @@ROWCOUNT!=0 BEGIN RAISERROR('THIS SCRIPT HAS ALREADY BEEN RUN ON THIS SERVER, CANNOT CONTINUE, PLEASE CONTACT DEV',11,1); RETURN; END GO insert into Product_Catalogue_History(Product_Catalogue_ID, Product_Catalogue_Status_ID, reason, MonthEnd) select pc.Product_Catalogue_ID, Last_Catalogue_Status_ID, 'Supplier Discontinued - Revert to previous status', GETDATE() from Product_Catalogue pc where product_catalogue_id in ( select product_catalogue_id from product_catalogue where supplier_id = 1 and Product_Catalogue_Status_ID = 4 and product_id in ( select product_id from product where product_code in ('S21/256GB/AB', 'S22/256GB/AB', '11/64GB AB', '11/64GB A+', '11 Pro/64GB AB', '11Pro Max/64GB AB', '12/64GB AB', '12/64GB A', '12Mini/64GB AB', '12Pro/128GB AB', '12Pro/256GB A', '12Pro/256GB A+', '12Pro Max/128GB AB', '12Pro Max/256GB AB', '13/128GB AB', '13/256GB AB', '13Mini/128GB AB', '13Pro/128GB AB', '13Pro/256GB AB', '13Pro Max/128GB AB', '13Pro Max/128GB A+', '14PRO/256GB AB', '8/64GB AB', '8 Plus/64GB AB', 'SE/128GB AB', 'SE/64GB AB', 'X/256GB AB', 'X/64GB AB', 'X/64GB A', 'XR/128GB AB', 'XR/64GB AB', 'XR/64GB A', 'XS/64GB AB', 'XR/64GB A', 'iPhone 11/64GB AB', 'iPhone 12/64GB A', 'iPhone 12/64GB AB', 'iPhone 8/64GB A+', 'iPhone 8 Plus/64GB AB', 'iPhone SE/128GB AB', 'iPhone SE/64GB AB', 'iPhone X/256GB AB', 'iPhone X/64GB AB', 'iPhone XS/64GB AB' )) ); update Product_Catalogue set Product_Catalogue_Status_ID = Last_Catalogue_Status_ID, Last_Catalogue_Status_ID = Product_Catalogue_Status_ID, Last_Catalogue_Changed = GETDATE(), Last_Change_Reason = 'Supplier Discontinued - Revert to previous status' where product_catalogue_id in ( select product_catalogue_id from product_catalogue where supplier_id = 1 and Product_Catalogue_Status_ID = 4 and product_id in ( select product_id from product where product_code in ('S21/256GB/AB', 'S22/256GB/AB', '11/64GB AB', '11/64GB A+', '11 Pro/64GB AB', '11Pro Max/64GB AB', '12/64GB AB', '12/64GB A', '12Mini/64GB AB', '12Pro/128GB AB', '12Pro/256GB A', '12Pro/256GB A+', '12Pro Max/128GB AB', '12Pro Max/256GB AB', '13/128GB AB', '13/256GB AB', '13Mini/128GB AB', '13Pro/128GB AB', '13Pro/256GB AB', '13Pro Max/128GB AB', '13Pro Max/128GB A+', '14PRO/256GB AB', '8/64GB AB', '8 Plus/64GB AB', 'SE/128GB AB', 'SE/64GB AB', 'X/256GB AB', 'X/64GB AB', 'X/64GB A', 'XR/128GB AB', 'XR/64GB AB', 'XR/64GB A', 'XS/64GB AB', 'XR/64GB A', 'iPhone 11/64GB AB', 'iPhone 12/64GB A', 'iPhone 12/64GB AB', 'iPhone 8/64GB A+', 'iPhone 8 Plus/64GB AB', 'iPhone SE/128GB AB', 'iPhone SE/64GB AB', 'iPhone X/256GB AB', 'iPhone X/64GB AB', 'iPhone XS/64GB AB' )) ); Print 'Insert into script history if everything else is cool'; Insert into Script_History (RunDate,Script_Name) Values (CURRENT_TIMESTAMP,'PhonesActive221224'); IF @@ERROR != 0 BEGIN RAISERROR ('UNABLE TO CONTINUE WITH SCRIPT', 11, 1); RETURN; END GO