Print 'Doing pre run Checks to ensure script not already run'; Select * from Script_History where Script_Name='RemoveTillRollsforFraStores'; 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 @isjv int; declare @SQL as varchar(5000); set @isjv= (select default_value from System_Default where Default_Name = 'IsJV'); select @isjv; if @isjv = 0 set @SQL = 'alter VIEW vwGetConsumables AS SELECT p.Product_ID, v.Division, v.Category, p.Product_Code, p.Description, h.Current_Cost, h.Current_Stock, 0 as OrderQuantity FROM dbo.Product AS p LEFT OUTER JOIN dbo.vwProductCategory AS v ON p.Product_Category_ID = v.Product_Category_ID LEFT OUTER JOIN dbo.HQ_Product_Stock AS h ON h.Product_ID = p.Product_ID WHERE v.Division = ''Expenses'' and p.HQ_Product_ID !=39639 and h.HQ_Branch_ID in (select top(1) CAST(Default_Value AS int) from System_Default where Default_Name = ''HQ_Store_Branch'');'; exec(@SQL) Print 'Insert into script history if everything else is cool'; Insert into Script_History (RunDate,Script_Name) Values (CURRENT_TIMESTAMP,'RemoveTillRollsforFraStores'); IF @@ERROR != 0 BEGIN RAISERROR ('UNABLE TO CONTINUE WITH SCRIPT', 11, 1); RETURN; END GO