MS SQL Stored Procedures Directly from Beckhoff TwinCAT PLC

MS SQL Stored Procedures Directly from Beckhoff TwinCAT PLC

Beckhoff TwinCAT 2 and 3 PLCs have the ability to execute SQL stored procedures to read or write data without the need for an HMI. This is achieved by setting up TwinCAT Database Server on the PLC to manage the database connections, and using functions from the TcDatabase library to execute stored procedures.

This blog will go through the steps to setup the TwinCAT Database Server and execute a pre-existing stored procedure.

1. Setup your SQL database and write your stored procedure.

2. Install TwinCAT Database Server on your programming PC.

3. Install TwinCAT Database Server on your Beckhoff CX controller. Navigate to C:\TwinCAT\Functions\TF6420-Database-Server on your programming PC. If using a Windows CE controller, setup a shared folder and copy the appropriate .CAB file onto your CX and run it. If using a CX with Windows Embedded CX, copy the Server folder to your CX and run the TcDatabaseSrv.exe.

4. Start up TwinCAT Database Server Configurator on your programming PC and add your database information.

5. Setup your route to the Beckhoff CX controller.

TwinCAT Database Server Configurator Settings

6.  For this example we will leave Startup in manual mode because we are manually triggering our stored procedures. More information about setting up TwinCAT Database server in AutoStart mode is available hereI also highly recommend creating an ErrorLogFile on the CX to provide more detailed error messages then the error codes given from the function blocks.

TwinCAT Database Server Configurator Settings

7. Upload the file to your CX controller.

8. Add the Tc2_Database library to your project.

9. Setup the input parameters for your stored procedure in an array of "ST_DBParemeter".


10. Execute the stored procedure using "FB_DBStoredProcedures".


11. A sample state machine which accomplishes steps 8 and 9 is available on Beckhoff's website here.

Learn more about DMC's PLC Programming services.


There are currently no comments, be the first to post one.

Post a comment

Name (required)

Email (required)

Enter the code shown above:

Related Blog Posts