You are here: Core Reference > Coding > Logic > Using an ST Program to Call an SQL Query

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:

NOTE: 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:


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.

NOTE: 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 : INT;
    • END_DATABASE_OBJECT;
  • END_TYPE
  • PROGRAM SQLTestHanddressPts
  • VAR
    • 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
      • 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>

NOTE: 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;

ClearSCADA 2015 R2