Connecting a Siemens S7 1200 & 1500 PLC with a SQL Server Database

In this tutorial video, we show step by step how to connect a Siemens S7 1200 & 1500 PLC with a Microsoft SQL Server database. To test the connection, we use a sample project.

Connecting a Siemens PLC to an SQL Database

The SQL4automation Connector acts as a bridge between your Siemens PLC and an SQL database. Once set up, you can send and receive data, run SELECT, INSERT, UPDATE, and DELETE queries, and execute stored procedures - all from your PLC program. The Connector runs on the same machine as your database server or on a separate device in the same network.

Prerequisites

  • SQL4automation Connector installed and configured for Siemens (Tutorial 1)
  • SQL Server running with a data source configured (Tutorial 5)
  • TIA Portal and the SQL4automation example project (download at sql4automation.com or via the link in the video description)

Setting Up Your TIA Portal Project

Device Configuration and IP Assignment

  1. Open the project, navigate to Device Configuration, and swap the default PLC for your model.
  2. Assign the PLC an IP address: go to Online Access → Update Accessible Devices → Functions → Assign IP Address.
  3. Set your PC's network adapter to the same subnet as the PLC (same first three octets, different last octet).
  4. Update the Ethernet port IP in the project to match, then Compile → Rebuild Hardware and download to the device.

Understanding the Program Structure

The example project is built around two execution contexts:

Main OB (every ~1 ms): Your user program - assembles SQL request strings into the global data block.
Cyclic Interrupt OB (every 10 ms): Processes requests and communicates with the Connector via the SFRA function block.

Four example functions are provided (SELECT, INSERT, UPDATE, DELETE) in both SCL and Ladder. Each maps to one SQL operation and stores its data in a dedicated instance data block. All share a common global data block (SQL for Siemens) that holds request strings, response data, and the execute interface.

Tuning the Cycle Time Parameter

When a request is active, the PLC stays in the SFRA block until it finishes - this can add 20+ ms of delay to your main program. Set the max cycle time parameter (e.g., 5 ms) to cap how long SFRA runs per cycle. This limits the delay at the cost of longer total request time. Tune it based on how time-sensitive your main program logic is.

Configuring the SQL4automation Connector

  1. Open the Connector Configurator and create a new link.
  2. Set a name and select Target Type: Siemens.
  3. Enter the Connector IP, port, and select your data source.
  4. Activate the test license - the connection should show as active.

Pointing the PLC at the Connector

Open the Cyclic Interrupt Block and enter:

  • Connector IP and port
  • Hardware ID of the PLC's Ethernet interface (find it under Device Configuration → System Constants)

Compile (Rebuild All) and download software to the device.

Testing with the Example Watch Tables

Each SQL example includes a prepared watch table. To run a test:

  1. Go Online and start monitoring.
  2. Set the execute variable to 1 - right-click the variable and use Modify Value or the keyboard shortcut.
  3. The request fires, the response populates, and the block resets to idle.

You can verify results live in SQL Server Management Studio alongside the PLC monitor.

Working Through All Four SQL Operations

SELECT returns rows from the database directly into the PLC's response buffer.

INSERT adds rows. It can also be used to copy data between tables - useful for creating backups before running destructive queries.

UPDATE modifies rows matching a condition. Always review the function block logic before executing - check that the WHERE clause targets only the rows you intend.

DELETE removes rows matching a condition. Same caution applies: verify the condition before executing.

Before running UPDATE or DELETE against live data, use INSERT to create a backup table. If something goes wrong, you can restore with a single INSERT … SELECT from the backup.

Next Steps: Stored Procedures

For queries you run repeatedly or want to update without touching PLC code, wrap the logic in a stored procedure. The PLC simply calls the procedure by name - all SQL logic stays on the database side and can be modified independently. This is covered in the next tutorial.

Supported Controllers

Switch to SQL4automation Now.

Connect industrial controllers directly with SQL databases. Field-proven since 2007 for maximum performance and security, completely without middleware.