Help
  • Explore Community
  • Get Started
  • Ask the Community
  • How-To & Best Practices
  • Contact Support
Notifications
Login / Register
Community
Community
Notifications
close
  • Forums
  • Knowledge Center
  • Events & Webinars
  • Ideas
  • Blogs
Help
Help
  • Explore Community
  • Get Started
  • Ask the Community
  • How-To & Best Practices
  • Contact Support
Login / Register
Sustainability
Sustainability

We Value Your Feedback!
Could you please spare a few minutes to share your thoughts on Cloud Connected vs On-Premise Services. Your feedback can help us shape the future of services.
Learn more about the survey or Click here to Launch the survey
Schneider Electric Services Innovation Team!

Accessing Data via SQL in Structured Text

Geo SCADA Knowledge Base

Access vast amounts of technical know-how and pro tips from our community of Geo SCADA experts.

cancel
Turn on suggestions
Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.
Showing results for 
Show  only  | Search instead for 
Did you mean: 
  • Home
  • Schneider Electric Community
  • Knowledge Center
  • Geo SCADA Knowledge Base
  • Accessing Data via SQL in Structured Text
Invite a Co-worker
Send a co-worker an invite to the portal.Just enter their email address and we'll connect them to register. After joining, they will belong to the same company.
You have entered an invalid email address. Please re-enter the email address.
This co-worker has already been invited to the Exchange portal. Please invite another co-worker.
Please enter email address
Send Invite Cancel
Invitation Sent
Your invitation was sent.Thanks for sharing Exchange with your co-worker.
Send New Invite Close
Top Labels
Top Labels
  • Alphabetical
  • database 32
  • Web Server and Client 31
  • WebX 19
  • Request Form 18
  • Lists, Events & Alarms 16
  • ViewX 15
  • Application Programming 12
  • Setup 12
  • Telemetry 8
  • Events & Alarms 7
  • Lists 7
  • Mimic Graphics 7
  • Downloads 6
  • Support 5
  • IoT 5
  • SCADA 5
  • Geo SCADA Expert 5
  • Drivers and Communications 4
  • Security 4
  • DNP 3 3
  • IEC 61131-3 Logic 3
  • Trends and Historian 2
  • Virtual ViewX 2
  • Geo Scada 1
  • ClearSCADA 1
  • Templates and Instances 1
  • Releases 1
  • Maps and GIS 1
  • Mobile 1
  • Architectures 1
  • Tools & Resources 1
  • Privacy Policy 1
  • OPC-UA 1
  • Previous
  • 1 of 4
  • Next
Latest Blog Posts
  • OPC UA - Driver and Server
  • Requirements for Generating a Valid OPC UA Server Certificate
  • Load Events Using LoadRecord and LoadRecords
  • Geo SCADA Embedded Component Licenses
  • Geo SCADA 2023 Known Issues
Related Products
product field
Schneider Electric
EcoStruxure™ Geo SCADA Expert

Invite a Colleague

Found this content useful? Share it with a Colleague!

Invite a Colleague Invite
Anonymous user
Not applicable
‎2021-06-09 04:02 PM
0 Likes
0
2825
  • Bookmark
  • Subscribe
  • Email to a Friend
  • Printer Friendly Page
  • Report Inappropriate Content

Link copied. Please paste this link to share this article on your social media post.

‎2021-06-09 04:02 PM

Accessing Data via SQL in Structured Text

Originally published on Geo SCADA Knowledge Base by Anonymous user | June 10, 2021 01:02 AM

📖 Home  Back  
In a structured text program you can define simple variables using %M, %I and %Q, however useful these are they are static.

There is a more dynamic way of reading data from ClearSCADA to use in a structured text program that returns the data in the form of a "resultset" (similar to ADO's RecordSet). This is defined using %S and a new TYPE declaration. It can only access data from within ClearSCADA, that is, you cannot use %S to run a query against an external database.

Due to the way that SQL commands within logic programs are handled, it is critical that sensible queries are used. Inappropriate queries can severely impact performance of the system and potentially delay other system operations.

 

TYPE

The first step is to define a suitable data type to store each row returned from the query. This is achieved using the TYPE keyword and derived using DATABASE_OBJECT or STRUCT keywords.

You would use DATABASE_OBJECT when accessing information from table derived from CDBObject Database Table. It allows read-write access to properties and the execution of methods (without parameters).

For example,

TYPE
Point : DATABASE_OBJECT(CPointAlgManual)
FullName : STRING;
CurrentValue : LREAL;
END_DATABASE_OBJECT;
END_TYPE


When using DATABASE_OBJECT it automatically includes the Id field as a column, so if you will be looking at a table without an Id Metadata Columns then the DATABASE_OBJECT cannot be used. The table defined (CPointAlgManual in the example) must be the table referred to in the SQL later. Anything derived from the base class CDBObject (i.e. all database objects) has an Id metadata column, however creating a column in a datagrid called Id does not count.

Also, if you plan on using a JOIN you'll not be able to use DATABASE_OBJECT. Instead you have to use STRUCT.

For example,

TYPE
Point :
STRUCT
Id : DINT;
FullName : STRING;
CurrentValue : LREAL;
END_STRUCT;
END_TYPE

Structurally, DATABASE_OBJECT and STRUCT are the same, however you cannot use methods in a STRUCT. A STRUCT does not assume anything, such as the Id column is included, and the STRUCT may point to any table within ClearSCADA, or multiple tables (JOINs). The order of the columns is important as they must be the same as in the SQL SELECT query.

Once you have your new TYPE defined you can declare the SQL to retrieve the resultset.

Declaration

The declaration of the resultset is similar to normal variables, it must exist within the VAR/END_VAR blocks only containing other external variables (%M, %I, %Q and %D).

For example,

VAR
Points AT %S(SELECT Id, FullName, CurrentValue FROM CPointAlgManual) : RESULTSET OF Point
END_VAR


The above declaration will work for both DATABASE_OBJECT and STRUCT examples of TYPE and will return all Internal Analogues and their current value.

You can also pass in parameters declared in the VAR_INPUT block.

For example,

VAR
Points AT %S(SELECT Id, FullName, CurrentValue FROM CPointAlgManual WHERE FullName LIKE '%' || ? OR FullName LIKE ? || '%') : RESULTSET OF Point WITH_PARAMS Name, Name;
END_VAR


The || is there to concatenate the % onto the start or end of the condition. If you just wanted a simple comparison then the following is suitable, even for strings:

VAR
Points AT %S(SELECT Id, FullName, CurrentValue FROM CPointAlgManual WHERE FullName LIKE ?) : RESULTSET OF Point WITH_PARAMS Name;
END_VAR

Using the Resultset

Once you have a resultset you can access its information. The following properties are available:

  • .Value - Allows access to column values.
  • .Valid (BOOL) - Indicates if the end of the resultset has been reached (similar to EOF).
  • .Size (DINT) - Number of rows in resultset.
  • .Idx (DINT) - Index of the current row.


The following methods are available to navigate the resultset:

  • .First()
  • .Last()
  • .Next()
  • .Prev()


For example,

WHILE Points.Valid DO
Count := Count + Points.Value.CurrentValue;
Points.Next();
END_WHILE;

 

The .Last() method moves the cursor to the last record, not after the end of the last record. This means that .Valid will still return True and if you were using .Last to break out of a WHILE loop then if the condition of the break happens on the last record, you could get into an infinite loop. You may have to call .Next() after a .Last() to go beyond the bounds of the resultset and .Valid() to become false. For example,

WHILE Points.Valid DO
IF Count > 100 THEN
Points.Last();
Points.Next(); (* Required else could cause an infinite loop after a few points had be summed up in the Count value*)
ELSE
Count := Count + Points.Value.CurrentValue;
Points.Next();
END_IF;
END_WHILE;

Things To Remember/Note

 

  • With the exception of VECTORs the STRUCT data structure is read-only, DATABASE_OBJECT supports writing to properties and calling methods.
  • STRUCT can point to any ClearSCADA table (historic, events, data grids, data tables, CDBObject derivatives) whilst DATABASE_OBJECT can only point to CDBObject derivatives.
  • The %S is a much larger performance hit (relatively) on the database compared to using %M, %I and %Q as it's the same as a normal SQL query. You can have many %S and TYPE declarations however the more %S you use, the slower the logic to initialise. Make sure you control your SQL to reduce the amount of data as much as possible using WHERE clauses and that the logic doesn't execute too often
  • If you forget to call .Next() in a WHILE loop then the logic will try to go into an infinite loop, however will error after a second or so with the "Too many opcodes exceeded message". It will keep trying to execute the logic program though and could cause system slowdowns.
  • The SQL used is the same as elsewhere in the product, so you can use UNIONS, JOINS, TOP, etc without problem, however UNIONS and JOINS need to use STRUCT instead of DATABASE_OBJECT.
  • When using linked tables a database write lock will be maintained whilst the data is collected. If the query to the target data source takes more than a second then performance of the ClearSCADA system may start degrade depending on how often that query is executed. Care should be taken when using linked tables. For more information on how logic executes refer to Logic Execution


Go: Home Back

Labels:
  • database

  • IEC 61131-3 Logic

Author

Biography

Anonymous user

Link copied. Please paste this link to share this article on your social media post.

  • Back to Blog
  • Newer Article
  • Older Article
To The Top!

Forums

  • APC UPS Data Center Backup Solutions
  • EcoStruxure IT
  • EcoStruxure Geo SCADA Expert
  • Metering & Power Quality
  • Schneider Electric Wiser

Knowledge Center

Events & webinars

Ideas

Blogs

Get Started

  • Ask the Community
  • Community Guidelines
  • Community User Guide
  • How-To & Best Practice
  • Experts Leaderboard
  • Contact Support
Brand-Logo
Subscribing is a smart move!
You can subscribe to this board after you log in or create your free account.
Forum-Icon

Create your free account or log in to subscribe to the board - and gain access to more than 10,000+ support articles along with insights from experts and peers.

Register today for FREE

Register Now

Already have an account? Login

Terms & Conditions Privacy Notice Change your Cookie Settings © 2025 Schneider Electric

This is a heading

With achievable small steps, users progress and continually feel satisfaction in task accomplishment.

Usetiful Onboarding Checklist remembers the progress of every user, allowing them to take bite-sized journeys and continue where they left.

of