Ai-OPs
ai-ops.com
Docs
/
Tags
/

Creating a Microsoft SQL Tag

Creating a Microsoft SQL Tag

After creating a tag on a Microsoft SQL device, you need to provide the SQL query that Koios will execute to read this tag's value. The create form does not include the query — set it on the tag's Configuration tab once the tag exists.

Protocol Fields

SQL Query

The SQL query that Koios executes against the database on each scan to read this tag's value.

  • Required for the tag to collect data
  • The query should return a single numeric value
  • Maximum length: 1,024 characters
SELECT TOP 1 temperature FROM sensor_readings
WHERE sensor_id = 'AHU-01'
ORDER BY timestamp DESC

Query Guidelines

Return a single value. Koios reads the first column of the first row of the result set. If your query returns multiple rows or columns, only the first value is used.

Keep queries lightweight. The query runs on every scan cycle, so avoid expensive joins, full table scans, or complex aggregations that could slow down the database.

Use parameterized values where possible. If you need the most recent reading, use TOP 1 with an ORDER BY on the timestamp column rather than scanning the entire table.

Query Restrictions

Only read queries are accepted. Koios rejects a query containing any of these keywords:

DELETE · CREATE · ALTER · DEFAULT · DROP · DATABASE · SET · TRUNCATE TABLE · UPDATE

A rejected query puts the tag into a configuration error — "Query contains a disallowed keyword" — and it never reads.

Example Queries

Latest sensor reading:

SELECT TOP 1 value FROM readings
WHERE tag_name = 'Supply_Temp'
ORDER BY timestamp DESC

Aggregated value:

SELECT AVG(temperature) FROM hourly_averages
WHERE zone = 'Warehouse-1'
AND timestamp > DATEADD(HOUR, -1, GETDATE())

Count of active alarms:

SELECT COUNT(*) FROM active_alarms
WHERE severity >= 3

After Configuration

  1. Save the tag configuration
  2. Test the tag: click
    Test
    to verify the query returns a valid value (see Testing a Tag)
  3. Enable the tag: flip the enable switch to start collecting data