This content is not available in your preferred language.

The content is shown in another available language. Your browser may include features that can help translate the text.

Execute SQL Stored Procedures in LabVIEW

Updated Sep 5, 2024

Environment

Software

  • LabVIEW
  • Database Connectivity Toolkit

When you are using the Database Connectivity Toolkit in LabVIEW, you can execute stored procedures saved in the SQL database, providing input parameters and reading a resulting recordset or output parameter. This article provides guidance on what functions can be used to accomplish this.

This article assumes you have the Database Connectivity Toolkit installed for your LabVIEW version and you have established a connection to a database already. If not, make sure the bitness of LabVIEW and the toolkit is the same as the database driver or provider you are using to connect to the database and refer to How to Connect LabVIEW to a Database for more information about connecting LabVIEW to databases.

  1. Open the connection to the database using the DB Tools Open Connection VI.
  2. Place the DB Tools Create Parameterized Query VI in the block diagram.
  3. Set the Stored Procedure? input terminal of the the DB Tools Create Parameterized Query VI to True
    • The parameters input terminal is an array of clusters. Each cluster contains a parameter name, type, direction and initial value, these cluster elements represent a column or field in the database table. 
  4. There are slightly different ways to execute the stored procedure and read its output depending on the parameter type to be used:


Using Input Parameters and reading resulting recordset

1. Enter {call storedprocedure_name (?,?)} in the SQL query input terminal of the DB Tools Create Parameterized Query VI.
  • The question marks correspond to the parameters that you are setting. Include a question mark for each parameter that you would like to work with, or set the auto-detect parameters? input terminal to True. In the above example the store procedure takes two input parameters (hence two questions marks are included in the query).
  • Automatically detecting parameters can be a time-consuming process and is not supported by all databases and/or database providers/drivers.
2. Set the input parameters values using the DB Tools Set Parameter Value VI.
3. Run the query with the DB Tools Execute Query VI.
4. Extract the recordset data with the DB Tools Fetch Recordset Data VI.
5. The Database Variant To Data function can be used to convert the variant data received to LabVIEW data types.
  • If the stored procedure does not have a return value or output parameter, it is not necessary to use the DB Tools Fetch Recordset Data VI and Database Variant To Data.
  • If only one element within the recordset is needed, you can use the DB Tools Fetch Element Data VI instead. This one would not need the Database Variant To Data function as it does not return a variant data type.
6. Clear the reference to the command-recordset and command references created by the previous functions with the DB Tools Free Object VI.
7. If there aren't any other actions to be taken close the connection to the database with the DB Tools Close Connection VI.

The following image shows an example of how to call a store procedure that uses two input parameters to return a recordset:

query.JPG