Using an ST Program to Call an SQL Query

You can code an ST program to retrieve information from the database via an SQL Query. The requested data is returned in a RESULTSET and can then be used in the ST Program's calculations.

Each Query returns a RESULTSET that has these variables:

The following methods can be used to navigate through the RESULTSET:

The SQL Query in an ST Program can only access data in the ClearSCADA database.

The syntax for calling an SQL Query in an ST program is:

TYPE

<Name of Type for RESULTSET>:<STRUCT or DATABASE_OBJECT>

<Member Name> : <Type>;

<Member Name> : <Type>;

<Method Name>: METHOD;

<END_STRUCT or END_DATABASE_OBJECT>;

END_TYPE

PROGRAM <Program Name>

VAR NOCACHE

<Var name> AT %S(SELECT <column 1>, <column 2>,<column 3> FROM <Database Table> WHERE <Filter>) :RESULTSET OF <Name of Type for RESULTSET>;

END_VAR


Where:

When you have declared the Type, Members, Methods, and SQL Query as required, you can use the RESULTSET variables and methods in your ST Program. Typically, they are used within WHILE...DO functions.

Do not enter the angle brackets < >. As with other syntax descriptions, you should not enter the angle brackets unless specifically instructed to do so.

Example 1:

The following example shows an ST Program that is used to search for internal analog points in a Group named 'Power Simulation'. When the program locates the internal points, it steps through each record and increases the corresponding point's current value by 1:

TYPE

Point : DATABASE_OBJECT(CPointAlgManual)

CurrentValue : LREAL;

CurrentState : USINT;

END_DATABASE_OBJECT;

END_TYPE

PROGRAM SQLTestHanddressPts

VAR NOCACHE

Pts AT %S(SELECT Id, CurrentValue, CurrentState FROM CPointAlgManual WHERE FullName LIKE 'Power Simulation.%') : RESULTSET OF Point;

END_VAR

WHILE Pts.Valid DO

Pts.Value.CurrentValue := Pts.Value.CurrentValue + 1;

Pts.Next();

END_WHILE;

END_PROGRAM

Example 2:

The following example shows an ST Program that is used to search for advanced outstations. When the program locates the outstations, it steps through each record and resets the corresponding outstation's communication statistics:

TYPE

Outstation : DATABASE_OBJECT( CAdvOutstation )

ResetCommsStatistics : METHOD;

END_DATABASE_OBJECT;

END_TYPE

PROGRAM ResetAllOsCommsStats

VAR NOCACHE

Os AT %S(SELECT Id FROM CAdvOutstation) : RESULTSET OF Outstation;

END_VAR

WHILE Os.Valid DO

Os.Value.ResetCommsStatistics();

Os.Next();

END_WHILE;

END_PROGRAM>

When you include SQL queries in ST programs, and use braces '{' and '}' within SQL statements, you should use the '^' escape character before each brace.

For example:

rsPoints AT %S(SELECT ID, CurrentValueFormatted, CurrentTime FROM CSIMPLEPOINT WHERE CURRENTTIME = ^{ OPC 'D' ^}): RESULTSET OF Point;


Disclaimer

ClearSCADA 2017 R2