IMD SolutionComputing LibraryShop main store

How-to

Pass PLC node data into a Node-RED MSSQL insert

Edge computing and IIoT Published 7 min read

Short answer

The PLC read node already emits your data on msg.payload; the missing piece is mapping those properties to named parameters in the MSSQL node's INSERT. Attach a Debug node to read each property path and type, build a payload object of named values, keep floats numeric, and prove the write by querying one known row back from the table.

On this page
  1. Which MSSQL node do I install in Node-RED?
  2. How do I see what the PLC read node actually sends?
  3. How do I map the PLC message into the insert values?
  4. How do I write the parameterized INSERT in the query window?
  5. Why do my floats arrive as text with quotes?
  6. How do I prove the insert actually wrote the row?
  7. Can this route run a SQL UPDATE too?
  8. What do I check when the SQL connection itself fails?
  9. Questions asked

You already have both halves working: the read node that pulls values from the controller, and the MSSQL node that runs a simple query against SQL Server. The missing piece is the mapping between them — the read node's msg.payload has to become a named set of parameters the INSERT consumes. Do it in four moves: install a proven MSSQL node, look at the real message with a Debug node, map each property to a named parameter, and prove the write by reading one known row back out of the table. Everything below uses a two-signal proof of concept — a tank level and a temperature — so you can swap in your own points.

Which MSSQL node do I install in Node-RED? #

Open Manage Palette from the menu at the top right of the editor, search for mssql, and install node-red-contrib-mssql-plus, which executes queries, stored procedures and bulk inserts against Microsoft SQL Server and Azure databases.

The search lists several packages; node-red-contrib-mssql is the one most often recommended, and node-red-contrib-mssql-plus is its extended variant. Both take their values from msg.payload, so the mapping in the next sections applies to either. Configure the connection once — server address, database name, login — and every query node on the palette reuses that configuration.

If the palette install itself fails on a locked-down edge box, the walkthrough of resolving Manage Palette install failures covers the usual causes and fixes.

How do I see what the PLC read node actually sends? #

Wire the read node straight into a Debug node set to show the complete msg object, fire one read, and read each value's property path and type off the debug sidebar; never assume the number sits in msg.payload, in the first array element, or under some named field.

Numbers print bare and strings print wrapped in double quotes, so the same glance tells you whether each value is numeric — the fact every later step depends on. A read node can emit an object, an array of table values, or one formatted string, depending on the points you selected; the sidebar shows the true shape, not the node's name. Node-RED's own message guide notes that the Debug node can report a message element's path and that you can paste that path straight into a Change node, which saves retyping.

How do I map the PLC message into the insert values? #

Put a Change node between the read node and the MSSQL node and build a new payload object whose keys are the parameter names, copying each value from the path the Debug node showed you; one key per column, matching the column list in the INSERT.

Keep the database node disconnected until step 5, and work against an example table named line1_tank with two float columns, level_pct and temp_c — replace both with your own.

  1. Fire one read and record every path and type. In this example the sidebar shows msg.payload.tank_level as the number 63.5 and msg.payload.tank_temp as the number 42.7, both bare numbers with no quotes.
  2. Add a Change node with one set rule per value, copying the source path to a parameter name: msg.payload.tank_level into msg.payload.level_pct, and msg.payload.tank_temp into msg.payload.temp_c.
  3. Add a second Debug node after the Change node and require exactly this object: {"level_pct": 63.5, "temp_c": 42.7} — two keys, both numeric.
  4. Write the INSERT in the database node's query field, one parameter per column, as the next section shows.
  5. Wire the Change node to the MSSQL node and deploy. The flow now persists in the runtime's flows file; if an editing session ever loses it, the guide to troubleshooting Manage Palette and lost flows walks the recovery.

How do I write the parameterized INSERT in the query window? #

In the node's query field, write the INSERT with a named parameter per column and send the values as a payload object whose keys carry those names; the node's documented example payload is {"count": 5, "age": 35}, two keys for two parameters.

In this node family the query references each payload key by name with a leading @, so the line1_tank example reads:

INSERT INTO line1_tank (level_pct, temp_c) VALUES (@level_pct, @temp_c);

The counts must agree: 2 columns, 2 markers, 2 keys — a mismatch surfaces as an error or as NULL in the new row. One safety note covers both ends: send values as bound parameters, never as text glued into the statement, and give the node a dedicated SQL login scoped to the one target table. The parameterized statement cannot be reshaped by the data, and the scoped login cannot turn a mapping mistake into a server-wide write.

Why do my floats arrive as text with quotes? #

Quotes in the Debug panel mean the value is a string, not a number, and a carriage return is only formatting; neither one inserts a row — only the MSSQL node executing the INSERT creates the record.

The quotes usually come from the read node handing over a formatted string, a line of CSV text with the carriage return included, or from an earlier Function node that stringified the payload. Fix it at the mapping stage, not in the database: if Debug shows the reading wrapped in quotes, convert it in the Change node's set rule with a JSONata $number() expression, or in a short Function node. SQL Server then binds a true float into a float column, while a varchar column would store the reading as text and break every later calculation — check the column types once, before the first insert.

How do I prove the insert actually wrote the row? #

Send exactly one known sample — level 63.5 percent and temperature 42.7 degrees Celsius — then query the table back and require one row carrying 63.5 and 42.7; a successful Debug output alone proves the node ran, not that the row landed.

Disconnect the PLC trigger and inject the sample manually, from an Inject node carrying the two numbers, so exactly one row is written. Then read it back through the same node, assuming the example table has an identity column named id:

SELECT TOP 1 level_pct, temp_c FROM line1_tank ORDER BY id DESC;

The proof passes only on an exact match: 1 row returned, level_pct 63.5, temp_c 42.7. A green success output from the MSSQL node is not the proof; it says the statement executed without error, and only reading the row back shows it landed with the values you sent.

Can this route run a SQL UPDATE too? #

Yes: the query window runs any statement you write, including UPDATE, while a no-code direct data link from the controller handles INSERT only — it appends new rows, or updates one column while blanking the rest — and cannot run an UPDATE statement.

Logging new samples needs nothing more than the INSERT above. The moment the application must correct or complete an existing row — a lab result attached after the fact, a value recorded wrong — write the UPDATE in the same query window with the same named parameters. That is also the dividing line against no-code alternatives: for logging, either route lands the rows; for revising existing data, only the Node-RED route qualifies.

What do I check when the SQL connection itself fails? #

Work the four connection inputs in order — server address, TCP port with SQL Server's default 1433 first, database name, and login — and confirm the server accepts SQL logins before you suspect the flow.

Because the node already ran a simple query, the connection was proven once; a failure that appears only after adding the insert is almost always mapping or permissions. For a genuine refusal, the guide to fixing ECONNREFUSED when Node-RED connects to a remote SQL database pairs each symptom with its fix. When the server drops out intermittently instead of refusing, hold the samples rather than discard them: the store-and-forward SQL buffering pattern keeps writes queued until the database answers again.

Questions asked

Can one insert load many rows at once, for a backfill?

The node-red-contrib-mssql-plus variant documents bulk insert support. Treat a big load as a test case, not a given: one public issue reports a flow pushing about 500,000 records in batches of 1,000 that ran into trouble. Chunk the backfill, time a single batch, and confirm each chunk lands before scaling the batch size up.

What SQL login should the database node use?

A dedicated login created for the node, not a shared engineering account and not the server's administrator account. Grant it INSERT and SELECT on the one proof-of-concept table: INSERT to write the samples, SELECT to run the read-back check, and nothing on any other table or database. Widen the grant only when a real application demands it, and record that change.

Does the same mapping work for MySQL or PostgreSQL?

The habit transfers unchanged: inspect the real message, map each property to a named value, keep measurements numeric, and prove one known row by reading it back. What changes is the package and its marker syntax. Install the node written for your database and use that node's documented marker, because the leading marker character differs between databases.

Where does my flow live once it is deployed?

In the flows file the runtime keeps on the host running Node-RED, alongside the credentials file that stores passwords separately. Treat both as plant data: copy them into your backup or version control after every deliberate change. If an editing session loses work, the runtime's file is the recovery point.

Keep reading

  • How-to

    Log production data from a panel PC to SQL Server

    Run Node-RED on the panel PC, read the production values straight from the controller, and insert one row per event into SQL Server with the mssql-plus node. Keep the panel's CSV-plus-FTP file relay only as a fallback, block duplicates with a last-record check and a unique key, and expect about 86,400 rows, roughly 8.6 MB, per day at one record per second.

    Edge computing and IIoT

  • How-to

    Recursively browse PLC nodes in Node-RED for InfluxDB

    A single read of the PLC root path returns that node's value only — nested levels are never collected. Browse the tree once to every leaf, cache the paths in Node-RED global context, then poll the cached list every 10 seconds and write one payload to InfluxDB with relative paths as field names. The deciding number is cycle duration: about 200 paths at a 20 ms mean read take 4.0 s, so the 10 s timer holds only while a measured cycle stays inside the interval.

    Edge computing and IIoT

  • How-to

    Route MQTT sensor data into Grafana on an edge PC

    An MQTT broker forwards each message once and keeps no history, so Grafana cannot trend from it directly. Subscribe with Node-RED, write each payload into InfluxDB, set a 365-day retention policy, and point a Grafana data source at that database. For the example below — 720 signals sampling every 5 s — that is about 0.9 TB for a year; provision roughly 2 TB.

    Edge computing and IIoT