This article explains how to execute stored procedures with a RD81MES96N MES Interface module based on an example using MS SQL Server 2017. Files used in the example can be found in attachments.
A stored procedure is a prepared SQL code that you can save in your database, so the code can be reused over and over again. So if you have an SQL query that you write over and over again, save it as a stored procedure, and then just call it to execute it. You can also pass parameters to a stored procedure, so that the stored procedure can act based on the parameter value(s) that is passed. The iQ-R MES Interface module RD81MES96N can access both input and output parameters of the procedure.
Prepare a stored procedure
Stored procedures can be quite complex and do a lot of things. For a simple example in this article we use a procedure that takes a table name as an input parameter, counts the number of rows in that table and returns it as an output parameter:
It can be executed directly on the server from SQL Server Management Studio using a query like this:
The first necessary steps to configure MES Interface (preparation on database side, installation of
MES Interface Function Configuration Tool, connecting to the module, configuring network settings and adding a database connection) are explained in this article:
Configure RD81MES96N(new version) module with MS SQL Server 2017
Add stored procedure configuration
To be able to use a stored procedure in your MES Interface application, it needs to be added to the project similar to a table. To do this navigate to Access Table/Proc. Settings in the MES Interface Function Configuration Tool, select a free row and click Edit:
In the configuration window set the name of your procedure (1), select the target database server (2) and switch from Access Table to Access Procedure (3). Then click Browse DB Procedure Information (4):
Another window will pop up. If you have a working connection to the database, it will display all available stored procedures:
After selecting the procedure, input and output parameters will be read automatically:
To use the configured procedure, first create tags to be used in your job:
In this example 4 tags are used:
- JobStartupRequest (Bit M0): used to trigger the job
- JobCompletionNotification (Bit M1): confirmation that the job was executed
- StoredProcedureParameter (Unicode String D0-D19): input parameter - name of the DB table we want to count
- StoredProcedureReturnedValue (Unsigned Word D20): output parameter - number of rows from the table
Job configuration
Add a job to execute the procedure:
Tags JobStartupRequest and JobCompletionNotification are used in the Trigger Conditions tab:
In Main-Processing tab add a DB Communication Action:
In the popup window select the Stored Procedure type (1), choose your procedure (2) and assign previosly configured tags to the procedure parameters (3):
After the configuration upload the project to your MES Interface module and the PLC project to your iQ-R CPU.
Testing the application
No additional programming is required in the PLC, all that is included in the sample is resetting the trigger variable after job completion:
To test your application, first write your database table name to the StoredProcedureParameter variable, then set the JobStartupRequest bit to trigger the job. After a successful execution the number of rows in that table should appear in the StoredProcedureReturnedValue variable.