1. SET ANSI_NULLS ON
  2. SET QUOTED_IDENTIFIER ON
  3. go
  4.  
  5.  
  6. ALTER PROCEDURE [dbo].[SP_Update_Device]
  7. -- Add the parameters for the stored procedure here
  8. @P_IEEE varchar(50), @P_Current_val Decimal, @P_Type int
  9. AS
  10.  
  11. BEGIN
  12. -- SET NOCOUNT ON added to prevent extra result sets from
  13. -- interfering with SELECT statements.
  14. SET NOCOUNT ON;
  15.  
  16. IF EXISTS (SELECT IEEE
  17. FROM Devices
  18. WHERE IEEE = @P_IEEE)
  19.  
  20. IF (SELECT MaxTemp FROM Devices WHERE IEEE = @P_IEEE) < @P_Current_val
  21.  
  22.  
  23. UPDATE Devices
  24. SET CurrentValue = @P_Current_val,
  25. Total = Total + @P_Current_val,
  26. Count_Total = Count_Total + 1,
  27. MaxTemp = @P_Current_val
  28. WHERE IEEE = @P_IEEE
  29.  
  30.  
  31. ELSE IF (SELECT MinTemp FROM Devices WHERE IEEE = @P_IEEE) > @P_Current_val
  32.  
  33.  
  34. UPDATE Devices
  35. SET CurrentValue = @P_Current_val,
  36. Total = Total + @P_Current_val,
  37. Count_Total = Count_Total + 1,
  38. MinTemp = @P_Current_val
  39. WHERE IEEE = @P_IEEE
  40.  
  41.  
  42. ELSE
  43.  
  44. INSERT INTO Devices (IEEE, FK_Type, CurrentValue, Total, Count_Total, MaxTemp, MinTemp)
  45. VALUES (@P_IEEE, @P_Type, @P_Current_val, @P_Current_val ,1, @P_Current_val, @P_Current_val);
  46.  
  47. END
  48.  
  49.