← Notes

SQL in automation: for records, not for control

SQL is a great bridge between factory floor and plant reporting, as long as you keep it out of the PLC cycle and design around UTC, keys and retention.

Somewhere in almost every plant I have seen, there is a database table nobody dares to touch—usually named something poetic like Data_Log_New_2. It has forty-two columns called Val1 through Val42, three years of rows, no primary key, timestamps stored in local German time with a magical duplicate hour every October when Daylight Saving Time ends, and a SCADA script inserting into it every five seconds. Everyone knows it is fragile. Nobody wants to be the person who breaks the morning production report.

SQL is one of the best tools an automation engineer can add to their kit, provided you keep clear about what it is for. SQL belongs in the record-keeping layer, not in the control loop.

Keep the database out of the PLC cycle

Modern PLCs can open TCP sockets and even send SQL queries directly. That does not mean you should make a machine wait for a database commit before closing a valve or indexing a conveyor. A SQL server is an IT-class component: it runs backups, rebuilds indexes, takes security patches, and occasionally locks a table for twelve seconds while someone runs an ad-hoc query from Excel.

Put an explicit integration layer between the controller and the database: a SCADA gateway (such as Ignition or WinCC), a small dedicated service, or a store-and-forward historian. The PLC updates a clean interface and keeps running the line; the gateway buffers writes locally if the database goes offline and drains the queue once the server returns.

Design tables around real questions, not tag dumps

Before writing a CREATE TABLE statement, write down the three questions people will ask six months from now. “What were the critical temperatures during batch 26-0412?” requires time-series data tied to equipment and batch windows. “How many units passed inspection on Line 2 yesterday?” requires discrete production records. Mixing both into one giant catch-all table makes every query slow and ugly.

A clean discrete production record table looks dull on purpose:

CREATE TABLE dbo.ProductionEvents (
    EventId          BIGINT IDENTITY(1,1) PRIMARY KEY,
    SourceEventKey   NVARCHAR(100) NOT NULL UNIQUE,
    OccurredUtc      DATETIME2(3)  NOT NULL,
    IngestedUtc      DATETIME2(3)  NOT NULL DEFAULT SYSUTCDATETIME(),
    LineCode         NVARCHAR(32)  NOT NULL,
    EquipmentCode    NVARCHAR(64)  NOT NULL,
    BatchNumber      NVARCHAR(64)  NULL,
    EventType        NVARCHAR(32)  NOT NULL,
    QuantityGood     INT           NULL,
    QuantityReject   INT           NULL,
    OperatorId       NVARCHAR(64)  NULL
);

CREATE INDEX IX_ProductionEvents_Line_Time
    ON dbo.ProductionEvents (LineCode, OccurredUtc DESC);

Notice a few small choices that prevent years of pain: two timestamps (when the event happened in UTC, and when the database received it), a unique source key so retries never create double counts, and a composite index aligned with how operators and engineers actually filter the table.

Where SCADA platforms like Ignition help—and where discipline still matters

Modern gateway-centric SCADA platforms such as Ignition make SQL integration almost effortless. Transaction groups, named queries, store-and-forward buffers and UDT-based tag models mean you can get reliable structured data from a PLC into PostgreSQL or SQL Server without writing fragile glue scripts.

The danger of an easy tool is that it also makes it easy to generate bad data at high speed. If your UDTs and tag naming standards are sloppy, you end up logging five inconsistent versions of machine state across five lines. Agree on equipment codes, enum definitions and UDT structure first; let the SCADA gateway mirror that structure cleanly into SQL.

Five SQL mistakes I keep running into

  • Storing local time instead of UTC. Every autumn DST switch creates a duplicate hour in your trends and batch timelines. Store UTC in the database and convert to local time in the UI or report.
  • Ignoring retention and archiving on day one. A table growing by 50,000 rows per day feels tiny during FAT and turns into a 55-million-row bottleneck three years later. Partition by month or define an archiving policy before go-live.
  • Building SQL strings by string concatenation inside SCADA scripts. Use parameterised queries or stored procedures, both for SQL-injection safety and so the database can reuse query plans.
  • Confusing SQL with an MES. Putting an SQL database behind SCADA gives you storage, not manufacturing execution workflows, material genealogy or controlled change history. I wrote more on that boundary in PLC, SCADA and MES.
  • Giving the SCADA service account db_owner privileges. Create a scoped service user with only the INSERT, SELECT or EXECUTE rights the gateway actually needs, and keep audit tables append-only.

When SQL is treated as a proper engineering interface—with UTC timestamps, explicit keys, local buffering and a retention plan—it stops being the fragile reporting sidecar nobody wants to touch and becomes the cleanest bridge between the factory floor and everything above it. If your pipeline also has to guarantee zero silent drops between two databases, see the SCADA export that quietly loses events.