IMD SolutionComputing LibraryShop main store

Explainer

S7-1500 direct SQL Server access over TDS: what it can and cannot do

Edge computing and IIoT Published 6 min read By IMD Solution engineering team

Short answer

Yes, within documented limits. An S7-1500 logs in to SQL Server over the TDS protocol with the Siemens LSQL function and runs predefined SELECT and INSERT statements held in a PLC string array, with no OPC server or PC in between. Receive-side decoding is reported incomplete, so commit to it for fixed inserts and hand simple SELECTs; put runtime-built SQL and mixed-type reads on a gateway.

On this page
  1. What the native TDS path is
  2. How a statement is run
  3. The receive side is the limit
  4. Sizing the string array for a 41-character SELECT
  5. Treat the login as captured
  6. Where the gateway still wins
  7. Questions asked

Yes, with limits you can state exactly. An S7-1500 can log in to SQL Server and run SQL statements over TDS, the protocol SQL Server itself speaks, using the LSQL library from Siemens support entry 109779336, published in 2020 and covering the S7-1200 as well. The statements are predefined SELECT and INSERT texts held in a PLC string array and chosen by index, with no OPC server and no PC in the data path. Two limits decide where you use it: the statement text is fixed at engineering time, and the receive side is reported incomplete, so a SELECT result can sit undecoded in a receive buffer. Fixed inserts from a cell: use it. Runtime-built SQL or mixed-type reads: use a gateway.

What the native TDS path is #

TDS, Tabular Data Stream, is SQL Server's own wire protocol, the same one its client drivers speak, so the controller talks to the database the way an application would. The support entry describes using TDS to log in to a SQL Server database and transmit SQL instructions, reading data from the database or sending data to it. On the controller, the LSQL library handles the login and the transfer, so the path runs CPU to database with nothing between: no OPC server, no .NET bridge, no gateway service to keep alive.

One property of the protocol matters later. Microsoft's protocol documentation states that a TDS data stream can span multiple network data packets, so the server's reply to a SELECT arrives as a stream the PLC side must reassemble and decode. That receive side is where this native path is weakest.

How a statement is run #

The model is engineering-time SQL with runtime-time selection. You configure the connection as the entry shows, with the server address and the database login the function needs; the field setup that demonstrated it used 172.16.62.1 as the SQL Server address. The statement texts live in a string array in a program block. At runtime your logic picks one statement by its array index, passes that index to the function's input and triggers execution; the demonstrated workflow was download the changes, activate the execution input, then deactivate it on the application's trigger.

Because every statement is compiled into the array, the PLC cannot compose SQL at runtime: no dynamic WHERE clause, no runtime table names, no values concatenated into the text. That is a constraint and a security property in one, since the controller can only ever send exactly the statements you built. If a statement must change while the machine runs, this function is the wrong tool; build that query on a gateway instead.

The receive side is the limit #

Field reports from engineers who ran the library describe the module as still under development: response data is left in the receive buffer InstLsql_Microsoft.statRcvData.bytes[], and you have to search for your information in it. A working login proves only the login. A SELECT that returns several records and mixed SQL types arrives as TDS tokens, and unless the PLC receive structure matches the column metadata exactly, the structured result stays empty or wrong while the connection looks healthy.

To see what actually came back, capture it: run Wireshark, execute the query — the field example was Select Amount from PLCDATA_2 where Fruit = $'Apple$', where the dollar signs are the PLC string's escapes for the apostrophes — filter the capture for TDS, open the SQL Server response frame and read Token – ColumnMetaData. That token is the server's own description of the result layout, and it is what your receive structure has to mirror.

Keep this connection out of safety logic: no safety function may depend on a database reply arriving.

Sizing the string array for a 41-character SELECT #

The case: one element of the statement array must hold the field example's lookup query, and you want the declaration and the memory it costs.

Inputs. The statement is SELECT _barcode FROM mytable WHERE id = 4, taken from the field example. The string format is the standard single-byte S7 layout: a string declared for at most n characters occupies n + 2 bytes — one byte for the maximum length, one for the current length, then n character bytes at one byte per character — and n can be at most 254.

Rule. Declare every array element at least as long as the longest statement, rounded up to leave editing headroom; the array then costs the element size times the element count.

Arithmetic. Count the statement: SELECT (6) + space (1) + _barcode (8) + space (1) + FROM (4) + space (1) + mytable (7) + space (1) + WHERE (5) + space (1) + id (2) + space (1) + = (1) + space (1) + 4 (1) = 41 characters. The tight declaration, String[41], occupies 41 + 2 = 43 bytes per element. The declaration to use, String[64], occupies 64 + 2 = 66 bytes and leaves 64 − 41 = 23 characters of headroom for a longer column or table name later.

Result. Each element of the array costs 66 bytes; an array of k statements costs k × 66 bytes of data-block memory.

Decision. Declare every element String[64]. Two thresholds follow. A statement that grows past 254 characters no longer fits an S7 string at all, so shorten it or move that query off the PLC. And a SELECT returning more than a couple of fixed columns is one you will have to hand-decode, so that read belongs on the gateway, not here.

Treat the login as captured #

Engineers using the library with TIA Portal V21 report that the TDS7 login carries user name and password in clear text, and that the SQL batch travels in clear text as well. Decide accordingly, in three moves. One: a dedicated SQL login that exists for this cell and nothing else, with rights only on the one target database. Two: a firewall rule that lets TCP port 1433, SQL Server's default port, reach the server from the CPU's address only. Three: keep the segment separated, because anything that can capture the cell network can read this credential. An encrypted-transport requirement is, by itself, the decision to use a gateway instead.

Where the gateway still wins #

Three cases, three decisions.

A handful of fixed INSERT statements pushing cell data into one table, with no readback you must trust: use the native function, declare the array as above, and confirm each inserted value once in the database before you rely on it.

Reading data back where the result mixes types or runs past a couple of columns, composing SQL at runtime, running UPDATE or DELETE, encrypting the credential, or keeping an audit trail of what was sent: keep the OPC path. Logging PLC values from an OPC server into SQL Server is exactly that design, and the PLC to SQL Server through a REST API or direct database access decision belongs on the PC beside it.

The decode-and-compose work belongs where it is maintainable: on a fanless mini PC as the cell gateway, not in PLC code that every firmware change puts at risk.

If your only PLC-side need is pushing fixed rows into one table, the native TDS function is enough, and simpler than anything with a middleware hop. The moment a requirement touches composing, decoding or encrypting, the answer is the gateway, not more PLC code.

Questions asked

Which rights should the SQL Server login have for the PLC?

The minimum that lets the statements run: a dedicated SQL login whose database user is member of db_datawriter on the one target database, plus db_datareader only when a SELECT must run. No server-wide rights, no sysadmin. Since the credential can be captured on the wire, anyone who grabs it should reach exactly those tables and nothing else.

Can the PLC run UPDATE or DELETE statements the same way?

The documented scope covers reading and sending data with predefined SELECT and INSERT statements, and the field examples show nothing beyond that. Do not put business-logic mutations on this path: a wrong statement executed from cyclic logic has no confirmation step. Route UPDATE and DELETE through the gateway, where the statement is built, logged and reviewed, until your library version documents them.

Does every SELECT need its own receive structure?

Yes. Each query returns its own column list and SQL types — the demonstrated result mixes varchar, Boolean and DateTime values — so one layout does not fit every result set. Adjust the typeUseCaseSpecificData structure in the SqlReceive data block for each query, and confirm the mapped fields against a packet capture before trusting them in production.

Is the native TDS path faster than an OPC server?

No measurement documents that. The native path removes a middleware hop and its polling cycle, but the call still executes within the PLC cycle and a person must decode each response by hand. Choose it to keep a fixed insert simple, not for speed; where throughput matters, measure both paths on the real cell network before committing.

Keep reading

  • Explainer

    Why a varbinary historian value cannot be decoded from SQL alone

    A varbinary column fixes storage capacity, not field layout, so the rows are the OPC UA server's private serialization and SQL alone cannot decode them. Read the history with OPC UA Historical Access (IEC 62541-11), or decode in SQL only when the vendor documents the layout for the installed build or controlled writes of 0x0000, 0x0001 and 0x0100 have pinned the offset and byte order.

    Edge computing and IIoT

  • Explainer

    Connect on-premise OPC UA and SQL servers to a cloud PI System

    Yes. Install the AVEVA adapters on-premise, on a small host next to the OPC UA and SQL servers, and point egress at PI Web API in the cloud; the PI Data Archive port is not a supported egress destination. Only the adapter-to-Web API direction needs an outbound route.

    Edge computing and IIoT

  • Explainer

    Do you need a historian or just an OPC tag server?

    No. An OPC tag server (OPC DA) supplies the current value, quality and timestamp of each item and stores nothing, so monthly figures need a historian or a SQL logger collecting from that same server. Keep a reporting client like ActiveFactory only if you also run the historian it expects.

    Edge computing and IIoT

Why historian CSV timestamps show only the date in Excel