Print 'Doing pre run Checks to ensure script not already run'; Select * from Script_History where Script_Name='DiscS200190625'; if @@ROWCOUNT!=0 BEGIN RAISERROR('THIS SCRIPT HAS ALREADY BEEN RUN ON THIS SERVER, CANNOT CONTINUE, PLEASE CONTACT DEV',11,1); RETURN; END GO declare @StoreId int; set @StoreId = (select default_value from system_default where Default_Name = 'Main_store_id'); If(@StoreId in (47,55,59,65,73,74,95,108,112,157,163,194,198,212,217,232,236,248,249,254,258,259,266,267,268,276,280,281,282,284,287,288,291,292,293,294,309,317,322,327)) begin insert into Product_Catalogue_History(Product_Catalogue_ID, Product_Catalogue_Status_ID, reason, MonthEnd) select pc.Product_Catalogue_ID, Product_Catalogue_Status_ID, 'Disc as per Ateeq', GETDATE() from Product_Catalogue pc where product_catalogue_id in ( select product_catalogue_id from product_catalogue where supplier_id = 1 and product_id in ( select product_id from product where hq_product_id in (41625)) ); update Product_Catalogue set Product_Catalogue_Status_ID = 3 , Last_Catalogue_Status_ID = Product_Catalogue_Status_ID, Last_Catalogue_Changed = GETDATE(), Last_Change_Reason = 'Disc as per Ateeq' where product_catalogue_id in ( select product_catalogue_id from product_catalogue where supplier_id = 1 and product_id in ( select product_id from product where hq_product_id in (41625)) ); end update SystemSetting set SettingValueString = '1,2,150,235,264,277' where SettingKey = 'AccessManagement'; Print 'Insert into script history if everything else is cool'; Insert into Script_History (RunDate,Script_Name) Values (CURRENT_TIMESTAMP,'DiscS200190625'); IF @@ERROR != 0 BEGIN RAISERROR ('UNABLE TO CONTINUE WITH SCRIPT', 11, 1); RETURN; END GO