Skip to main content
Version: 7.9

SQL Query Binding

Inductive University

SQL Query Binding

Watch the video

Binding Properties to a SQL Query

The SQL Query binding is a polling binding type that will run a SQL Query against any of the database connections configured in the Gateway. It is very similar to the DB Browse binding type in that both query a database to return data. The difference is the SQL Query Binding can manually be modified. This is useful for complex queries where you will use the more advanced functions of the SQL language that can not be accomplished with the DB Browse binding.

tip

The query that gets generated by the DB Browse will transfer over to the SQL Query binding when you switch the binding type. It may be useful to build the basic query structure with DB Browse first, then switch to SQL Query binding to modify the query to fit your needs.

Dataset Binding

The majority of SQL Query bindings will return a dataset (and are usually Dynamic). These will usually return many rows with multiple columns. For example, showing all customer details from a certain account, or all downtime events in the facility. The only important distinction here is that if you are returning multiple rows and/or columns, you need to put that binding on a property of type dataset like the Data property on a Table component. Basic data types like integers or strings can only show the first value of a query result, not the whole set. You can easily see the difference in the binding window because only basic data types will show two query entry fields, SELECT and UPDATE. Dataset properties only show SELECT.

SQL Query Binding: Dataset Binding

Dynamic Filters

Inductive University

SQL Query Binding - Dynamic Filters

Watch the video

Using the brace notation, you can include the values of component properties (within the same window) and Tag values inside your query. This is a very common technique to make your query dynamic. The values of the property or Tag represented are simply substituted into the query where the braces are.

Note that because the substitution is direct, you'll often need to add quotes to literal strings and dates to make your query valid. If you're getting errors running your query complaining about syntax, it is important to realize that these errors are coming from the database, not from Ignition. Try copying and pasting your query into the Query Browser and replacing the braces with literal values.

Example

A common requirement is to have a query filter its results for a date range. You can use the Date Range component or a pair of Popup Calendar components to let the user choose a range of dates. Then you can use these dates in your query like this:

SQL - SQL Query Binding with Parameter References
SELECT
t_stamp, flow_rate, amps
FROM
valve_history
WHERE
t_stamp >= '{Root Container.DateRange.startDate}' AND
t_stamp <= '{Root Container.DateRange.endDate}'

Notice the single quotes around the braces. This is because when the query is run, the dates will be replaced with their literal evaluations. For example, the actual query sent to the database might look like this:

SQL - SQL Query Binding with the Values Replaced
SELECT
t_stamp, flow_rate, amps
FROM
valve_history
WHERE
t_stamp >= '2010-03-20 08:00:00' AND
t_stamp <= '2010-03-20 13:00:00'

It is important to use single quotes and not double quotes (t_stamp = "2010-03-20 08:00:00") because these mean something different in certain databases like Microsoft SQL Server.

Scalar Query Update

Inductive University

SQL Query Binding - Scalar Query and Update

Watch the video

You can bind an input component bidirectionally to a SQL query to allow data be retrieved and updated in the database. This can only be done with a Scalar Query on a non Dataset property. Essentially, we mimic the bidirectionality of Tag and property bindings by adding in an update query to run whenever a value gets entered into the property with the binding. In our update query, we use the special parameter {this} to denote the new value from the bound property. If {this} is a string, it needs single quotes around it.

SQL Query Binding: Scalar Query Update

Take a Text Field with a simple query on it.

SQL - Simple Select Query
SELECT Name FROM area WHERE ID = 1

This will return a single value that can populate our text field. We then enable the Update Query at the bottom of the Property Binding window, and add in the update query.

SQL - Using Ignition's 'this' Keyword
UPDATE area SET Name = '{this}' WHERE ID = 1

After confirming the binding, we can see that our text field contains the value from the database and will update the database cell if we enter in a new value into the text field. This is a good way to alter very specific cells in a database table.

It works for DB Browse too!

The DB Browse Binding also features a database writeback that provides the same functionality as the SQL Query binding update query, and allows you to make your DB Browse Queries bidirectional as well, as long as your binding is a scalar query on a non dataset property. The update query on the DB Browse binding will also transfer to the SQL Query binding much like the select query, to help you get started with building your query!

Scalar Query Fallback

Inductive University

SQL Query Binding - Scalar Query and Fallback

Watch the video

If the property that is being bound is a scalar datatype (that is, not a Dataset), the value in the first column in the first row of the query results is used. If no rows were returned, the binding will cause an error unless the Use Fallback Value option is selected. The value entered in the fallback value text box will be used when the query returns no rows.

When binding a Dataset to a SQL Query, no fallback value is needed, because a Dataset will happily contain zero rows.

SQL Query Binding: Scalar Query Fallback

Stored Procedures

While queries can manually be written on a SQL Query binding, SQL Stored Procedures may also be called from a SQL Query Binding. Note, that the exact syntax is highly dependent on the type of database you are using.

For example, calling a Stored Procedure from MySQL would involve using the CALL command, while SQL Server utilizes the EXEC command.

SQL - MySQL Stored Procedure Call
CALL retrieve_daily_total
SQL - SQL Server Stored Procedure Call
EXEC retrieve_daily_total

For more information, please see the SQL Stored Procedures page.