Print 'Doing pre run Checks to ensure script not already run'; Select * from Script_History where Script_Name='SparesToExclusionList'; if @@ROWCOUNT!=0 BEGIN RAISERROR('THIS SCRIPT HAS ALREADY BEEN RUN ON THIS SERVER, CANNOT CONTINUE, PLEASE CONTACT DEV',11,1); RETURN; END GO if not exists (select * from ReorderExclusionList where Product_Code = 'WSDJ1201') begin insert into ReorderExclusionList (Product_Code,Active) values('WSDJ1201',1) end Go if not exists (select * from ReorderExclusionList where Product_Code = 'WSDJ1501') begin insert into ReorderExclusionList (Product_Code,Active) values('WSDJ1501',1) end Go if not exists (select * from ReorderExclusionList where Product_Code = 'WSPX15') begin insert into ReorderExclusionList (Product_Code,Active) values('WSPX15',1); end Go if not exists (select * from ReorderExclusionList where Product_Code = 'CDSPX15') begin insert into ReorderExclusionList (Product_Code,Active) values('CDSPX15',1); end Go if not exists (select * from ReorderExclusionList where Product_Code = 'TSDJ1501') begin insert into ReorderExclusionList (Product_Code,Active) values('TSDJ1501',1) end insert into Product_Catalogue_History (Product_Catalogue_ID, Product_Catalogue_Status_ID, reason, MonthEnd) select pc.Product_Catalogue_ID, PC.Last_Catalogue_Status_ID, 'Reversal of Revert to Untried, due to non customer items', GETDATE() from Product_Catalogue pc where product_catalogue_id in (select product_catalogue_id from product_catalogue WHERE PRODUCT_ID IN (select top 1 Product_ID from product where HQ_Product_ID in ( 3798,3799,3801,3802,3803) ) and Product_Catalogue_Status_ID in (7) and Last_Change_Reason = 'Revert to Untried, due to non customer items' and Supplier_ID=1 ); update Product_Catalogue set Product_Catalogue_Status_ID = 1 , Last_Catalogue_Status_ID = Product_Catalogue_Status_ID,Last_Catalogue_Changed= GETDATE(), Last_Change_Reason = 'Reversal of Revert to Untried, due to non customer items' where Product_ID = (select top 1 Product_ID from product where HQ_Product_ID in ( 3798,3799,3801,3802,3803) ) and Product_Catalogue_Status_ID in (7) and Last_Change_Reason = 'Revert to Untried, due to non customer items' and Supplier_ID=1 ; Print 'Insert into script history if everything else is cool'; Insert into Script_History (RunDate,Script_Name) Values (CURRENT_TIMESTAMP,'SparesToExclusionList'); IF @@ERROR != 0 BEGIN RAISERROR ('UNABLE TO CONTINUE WITH SCRIPT', 11, 1); RETURN; END GO