Print 'Doing pre run Checks to ensure script not already run'; Select * from Script_History where Script_Name='iPhonesActive111224'; 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 ('11Pro/256GBA', '11Pro/256GBB', '11Pro/64GBB', '11/128GBA+', '11/128GBAB', '11/256GBAB', '11/64GBB', '12/128GBAB', '12/64GBA+', '8/64GBA', 'X/256GBB', 'X/64GBA+', 'X/64GBB', 'XR/64GBA+', 'XS/64GBA', 'XS/64GBB' )) ); 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 ('11Pro/256GBA', '11Pro/256GBB', '11Pro/64GBB', '11/128GBA+', '11/128GBAB', '11/256GBAB', '11/64GBB', '12/128GBAB', '12/64GBA+', '8/64GBA', 'X/256GBB', 'X/64GBA+', 'X/64GBB', 'XR/64GBA+', 'XS/64GBA', 'XS/64GBB' )) ); Print 'Insert into script history if everything else is cool'; Insert into Script_History (RunDate,Script_Name) Values (CURRENT_TIMESTAMP,'iPhonesActive111224'); IF @@ERROR != 0 BEGIN RAISERROR ('UNABLE TO CONTINUE WITH SCRIPT', 11, 1); RETURN; END GO