Posts Tagged ‘Temperature’

Last temperature reading

September 13th, 2009

If you want to create a webpage or something similar you might want to show off the latest temperature reading.

This script will give you last reading in every table, if you want to exclude / include specific tables look at the “AND Name NOT LIKE” lines.

IF EXISTS(SELECT TABLE_NAME FROM TEMPDB.INFORMATION_SCHEMA.TABLES
      WHERE TABLE_NAME LIKE '#TemperatureLast%')
   DROP TABLE #TemperatureLast
GO

CREATE TABLE #TemperatureLast (
   MSureName SYSNAME NOT NULL,
   TimeStamp DATETIME NOT NULL,
   Temperature DECIMAL(18, 3) NOT NULL,
)

DECLARE
   @query VARCHAR(2000),
   @currTable SYSNAME

DECLARE tablesCurr CURSOR
	FOR
	SELECT Name as TableName
		FROM  sysobjects
		WHERE xtype = 'U'
			AND Name NOT LIKE '%space%'
			-- AND Name NOT LIKE '%something%'
			-- AND Name LIKE 'Temperature%'
		ORDER BY TableName
	FOR READ ONLY

OPEN tablesCurr
   FETCH NEXT FROM tablesCurr INTO @currTable
   WHILE (@@FETCH_STATUS <> -1) BEGIN

		SET @Query = 'SELECT TOP 1 ''' + @currTable + ''', TimeStamp, Temperature FROM ' + @currTable + ' ORDER BY TimeStamp DESC'
		INSERT #TemperatureLast EXEC (@query)

      FETCH NEXT FROM tablesCurr INTO @currTable
   END
CLOSE tablesCurr
DEALLOCATE tablesCurr

SELECT * FROM #TemperatureLast
DROP TABLE #TemperatureLast