Print 'Doing pre run Checks to ensure script not already run'; Select * from Script_History where Script_Name='RStoreupdateLaptopCondition'; 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 (243, 182, 35, 174, 227, 246, 276, 135, 25, 183, 9, 82,2)) begin update buyshop_trans_line set item_condition_id =2 where buyshop_trans_line_id in ( select buyshop_trans_line_id from stock_item where stock_item_id in ( select stock_item_id from stock_item_count where stock_item_id in ( select stock_item_id from stock_item s left join buyshop_trans_line btl on btl.buyshop_trans_line_id = s.buyshop_trans_line_id left join product p on p.product_id = s.product_id left join vwproductcategory v on v.product_Category_id = p.product_category_id where item_stock_type_id in (1,2,3,7) and composite like '%laptop%' and item_condition_id !=2) and stock_holding_group_id in (1,2) and stock_count >0)); end Print 'Insert into script history if everything else is cool'; Insert into Script_History (RunDate,Script_Name) Values (CURRENT_TIMESTAMP,'RStoreupdateLaptopCondition'); IF @@ERROR != 0 BEGIN RAISERROR ('UNABLE TO CONTINUE WITH SCRIPT', 11, 1); RETURN; END GO