Back

Topic

[KB1255]Repeat Query for Sql variables

Tags: Sql variables, UDC

16 minutes ago
By HOTE
Options
Print
Applies to:
PcVue 15.1 onwards
Summary:
PcVue 15 introduces the possibility to create Sql variables based on Sql queries. Version 15.1 introduces the ability to manually trigger these queries by manually entering parameter values. This article proposes a use case of these mechanisms. The repeat query option triggers automatically a query when the number of records received is greater than or equal to a constant value, instead of waiting for the configured trigger. Ex: Data avalanche or restart of PcVue after a downtime.
Details:

We have a query execution defined to be repeated every 10 seconds and with a maximum received row count equal to 100:

Then in the query itself, we add conditions according our execution plan. We want to retrieve the last 100 values since the last retrieved value:

images kbase KB1105 Query.png
images kbase KB1105 Query.png

About the query:

SELECT TOP (100) * FROM [TrendTable1]

WHERE   
([Chrono]> ?Param01)
OR
([Chrono]=?Param01 AND [Name]>’?Param02‘)
OR
([Chrono]=?Param01 AND [Name]=’?Param02‘ AND [Value]>?Param03)
OR
([Chrono]=?Param01 AND [Name]=’?Param02‘ AND [Value]=?Param03 AND [Quality]>?Param04)

ORDER BY [Chrono] ASC, [Name] ASC, [Value] ASC, [Quality] ASC

TrendTable1: Name of the table
Chrono: Timestamp of the variable in the table
Name: Name of the variable in the database
Value: Value of the variable in the database
Quality: Quality of the variable in the database
Order: You need to sort (ASC) the result in order to retrieve the data chronologically

In the WHERE condition, we use dynamic parameters and we store them in PcVue Sql variables:

Param01Param02Param03Param04
Last ChronoLast NameLast ValueLast Quality
SQL.Query01.LastChronoSQL.Query01.LastNameSQL.Query01.LastValueSQL.Query01.LastQuality

 Creation of these variables:

1- Param01 corresponds to the chrono, so we need to create a text variable

images kbase KB1105 Last chrono data selection.png
images kbase KB1105 Last chrono data selection.png

In Row selection: Select the last Row:

images kbase KB1105 Last chrono row selection.png
images kbase KB1105 Last chrono row selection.png

 In Mapping: Select FieldValue(“Chrono”) for Value

images kbase KB1105 Last chrono mapping.png
images kbase KB1105 Last chrono mapping.png

In parameters: Select =Value for ?Param01

images kbase KB1105 Last chrono parameters.png
images kbase KB1105 Last chrono parameters.png

In advanced tab of the variable, Check Save

images kbase KB1105 Last chrono advanced.png
images kbase KB1105 Last chrono advanced.png

2- Param02 corresponds to the Name, we also need to create a text variable similar to the previous one.
-Map the variable:
– In Row selection: Select the last Row
– In Mapping: Select FieldValue(“Name”) for Value In parameters: Select =Value for ?Param02
– Advanced Tab : Check Save

3- Param03 corresponds to the Value, we need to create a register variable.
– Map the variable:
– In Row selection: Select the last Row
– In Mapping: Select FieldValue(“Value”) for Value In parameters: Select =Value for ?Param03
– Advanced Tab : Check Save

4- Param04 corresponds to the Quality, we also need to create a register variable.
– Map the variable:
– In Row selection: Select the last Row.
– In Mapping: Select FieldValue(“Quality”) for Value In parameters: Select =Value for Param04
– Advanced Tab : Check Save

    The attached sample project shows an example of using SQL queries attached to SQL connections defined in PcVue. In particular, it shows a possibility to repeat the execution of these queries automatically under certain conditions.

    First of all, you need to enter the correct login and password for the SQL connection declared in the general part of the Application Explorer:

    images kbase KB1105 SqlConnection credentials.png
    images kbase KB1105 SqlConnection credentials.png

    Check that the SV DbConnect service has been started:

    images kbase KB1105 SvDbConnect service.png
    images kbase KB1105 SvDbConnect service.png

    The query will be launched every 10 seconds. It will retrieve all the information from the TRENDTABLE1 table by packets of 100 rows, starting from the last information retrieved previously.

    After the development of such a query, it is mandatory to run it at least once by manually entering the parameters. For instance if we want to get all data:

    images kbase KB1105 Trigger query with parameters.png
    images kbase KB1105 Trigger query with parameters.png

    Warning: if the database size is important you should not use 0 for the field ?Param01 (Chrono) but an acceptable value instead.

    Then, since we save the value of the internal variables that are used both to define the parameters of the query and to store their new value after each execution, the system works continuously.

    If we look at the trend curve, we can see that the variable SQL.Query01.F1R1_TEMP only changes value every 10 seconds, at the rate of the SQL query. But in fact it changes value several times and all these changes are recorded with the timestamp coming from the TRENDTABLE1 table. So when we press the refresh button, we see a nice smooth ramp instead of stairs in real time.

    images kbase KB1105 Sample project.png
    images kbase KB1105 Sample project.png

    In order to record all the changes of values that can exist since the last execution of the query, we should not use a filtering period while we configure the trend for SQL.Query01.F1R1_TEMP variable, as explained in the Sql Variables and Data Archiving article.

    Sample project:

    RepeatSqlQuery.zip

    Created on: 21 Oct 2025