How To: Use RunDynamicSQL() and GetDynamicSQLStatus() functions
14 Sep 2026
Overview
This article explains how to use the RunDynamicSQL() and GetDynamicSQLStatus() functions. These functions allow a crimson application to execute SQL statements dynamically during runtime and monitor the execution status of the database transaction.
Examples of use:
Insert tag values into a SQL table
Update an existing SQL record
Delete records
Build an SQL statement using Crimson tag values
Execute a stored procedure
Monitor whether a dynamically generated SQL statement completed successfully
APPLICABLE PRODUCTS
Crimson 3.2 Devices (build 3.2.1030 and later)
Crimson Control
SQL Query functionality
PRE-REQUISITES
Crimson 3.2 build 3.2.1030 and later
Note: RunDynamicSQL() and GetDynamicSQLStatus() were introduced in Crimson 3.2.1030 build.
The returned value should be stored in an integer tag or variable
Syntax - GetDynamicSQLStatus()
GetDynamicSQLStatus(SQLHandle)
The handle returned by RunDynamicSQL() is supplied to GetDynamicSQLStatus()
Please Note:
*The SQL handle is used to track an outstanding dynamic SQL request. Once the SQL operation has completed and a non-zero status has been returned, the handle no longer provides useful status information. Therefore, calling: GetDynamicSQLStatus() again after the handle has already completed can result in a -1. This can make an otherwise successful SQL transaction appear to have failed if the application does not preserve the first non-zero result.
Create program using RunDynamicSQL() and GetDynamicSQLStatus()
Example:
For this example, we are running the program in the background. Set Execution task to Background 1. Set External Date = Read when referenced.
Translate program when finished constructing. Right click on the program in Navigation Pane and select Translate
Step 3 - Initiate Program
For this example, I have added the program to an action button on a display page
This button is configured to run the program whenever released.
You can also add the program to the display page Actions properties. Right click on a blank space on any display page
Step 4 - Test Functionality
Load Crimson database to device using link options function
Once database is on device, use the HMI display or the Web Server to run the program
View results on your web display. For this example, we added the SQLStatus tag and the SQLStatusText tags to my display page as well as the tag1, tag2, and tag3 data being sent to the database.
Once you have confirmed success from the SQLStatus check your database to confirm the program ran properly and the appropriate tables have been updated according to your program.
Troubleshooting Tips
SQL Status = 1 (Success)
The SQL statement completed successfully.
Verify the expected records were inserted, updated, or deleted in the database.
Confirm the correct database and table were targeted.
SQL Status = -1 (Communication Failure)
Possible Causes
SQL Server is unreachable.
Incorrect server IP address or hostname.
Incorrect TCP port (typically 1433).
Firewall blocking database communications.
Recommended Checks
Verify SQL Query connection settings.
Confirm the SQL Server is running and accessible.
Test network connectivity between the Crimson device and SQL Server.
SQL Status = -2 (Query Pending)
Possible Causes
The SQL operation is still being processed.
Recommended Checks
Continue monitoring the status until a final result is returned.
Verify the SQL server is responding normally and is not heavily loaded.
SQL Status = -3 (SQL Error)
Possible Causes
Invalid SQL syntax.
Incorrect table, column, or stored procedure name.
Missing parameters or data type mismatch.
Insufficient database permissions.
Recommended Checks
Execute the generated SQL statement directly in SQL Server Management Studio (SSMS).
Verify all table names, column names, and stored procedure parameters.
Confirm the SQL user account has appropriate permissions.
Status Changes to -1 After a Previous Success
Once a SQL request has completed, the associated handle should no longer be queried. Continuing to call GetDynamicSQLStatus() on a completed handle may return -1, even though the transaction was successful.
Recommendation: Save the first non-zero status and reset the handle after processing the result.