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 Geo SCADA Expert 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 caret '^' escape character before each brace.

For example:

VAR NOCACHE

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

END_VAR

If you reference system status tags in your ST programs, you need to prefix the names of those system status tags with an additional caret '^' escape character. This escapes the reserved caret '^' character that is used at the start of the names of system status tags (also known as 'status attributes') (see Access System Status Information).

For example:

VAR

sServerStateTime AT %M(^^<server name>.ServerStateTime): DATE_AND_TIME;

END_VAR


Disclaimer

Geo SCADA Expert 2019