Posts Tagged ‘MSure’

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

Migrate data from MySQL to SQL Server

September 13th, 2009

Since I work with MS SQL Server (and have done for the last 10 years) I converted the MySQL-databases in MSure to MS SQL.
This script will copy 4 tables, easy to add more.
Remember to change the ConnectionString’s to your settings. (server, database, user and password)

Set oMySQL = createobject("ADODB.Connection")
oMySQL.ConnectionString = "DRIVER={MySQL ODBC 5.1 Driver};SERVER=localhost;DATABASE=msure;UID=msure;PWD=abc123; OPTION=3"
oMySQL.Open

Set oSql = createobject("ADODB.Connection")
oSql.ConnectionString = "DRIVER={SQL Server Native Client 10.0};SERVER=localhost;DATABASE=msure;UID=MSure;PWD=abc123; OPTION=3"
oSql.Open

CopyTable "UtomhusNorr"
CopyTable "UtomhusOster"
CopyTable "UtomhusSoder"
CopyTable "UtomhusVaster"

sub CopyTable(sTable)
  set oSource = oMySQL.Execute("SELECT * FROM " & sTable)
  do while not oSource.Eof

    sSql = "INSERT INTO " & sTable & " (TimeStamp, Temperature) VALUES ('" & oSource("TimeStamp") & "', '" & Replace(oSource("Temperature"),",", ".") & "')"

    wscript.echo sSql
    oSql.Execute sSql

    oSource.MoveNext
  Loop
End Sub

oMySQL.Close
oSql.Close