日本語 English

How to Execute Stored Procedures from a PLC

What Is a Stored Procedure?

A stored procedure groups a series of operations on a database
In SQL Server, system stored procedures can be found in the location below

Benefits of executing stored procedures from a PLC

There is no need to build complex SQL statements on the PLC side.

Processing can be consolidated on the database side, keeping the PLC program simple.

Search, registration, update and other processing can be reused as stored procedures.

Stored procedure list in SQL Server Management Studio

Example

An example that calls the "sp_databases" system stored procedure, which gets the list of databases

Ladder logic

Ladder program for executing a stored procedure from a PLC

Turning the Execute flag ON starts the process of calling the stored procedure

  1. DB_Connect: connects to the DB
  2. DB_AttachProcedure: specifies the stored procedure and gets a handle
  3. DB_ExecuteProcedure: executes the stored procedure
  4. DB_DetachProcedure: releases the handle
  5. DB_Close: closes the connection to the DB

The parameters needed for DB_AttachProcedure are as follows

  • ProcName: procedure name
  • ArgIn: input parameters
  • ArgOut: output parameters
  • ArgInOut: in-out parameters
  • ReturnVal: return value
  • ResultSet: result set

ArgIn, ArgOut and ArgInOut are set to the input/output parameters specified by the stored procedure
If none are specified, enter _DBC_Unused
The results of the stored procedure are output to ReturnVal and ResultSet

Monitoring the stored procedure execution result

FAQ

What is a stored procedure?

A set of processing stored in the database.

How is it different from executing SQL statements directly from the PLC?

Complex processing can be moved to the database side, improving maintainability.

Can input parameters be passed?

Yes, using the ArgIn parameter.

Can return values be obtained?

Yes, with ReturnVal and ResultSet.

Download

Comments