Help
  • Get started
  • Ask the Community
  • How-To & Best Practices
  • Contact Support
Login / Register
Brand Logo
Help
  • Get started
  • Ask the Community
  • How-To & Best Practices
  • Contact Support
close
  • Community Home
  • Forums
    • By Topic
    • By Topic
      EcoStruxure Building
      • Field Devices Forum
      • SmartConnector Forum
      EcoStruxure Power & Grid
      • Gateways and Energy Servers
      • Metering & Power Quality
      APC UPS, Critical Power, Cooling and Racks
      • APC UPS Data Center & Enterprise Solutions Forum
      • APC UPS for Home and Office Forum
      EcoStruxure IT
      • EcoStruxure IT forum
      • EcoStruxure IT™ Advisor CFD
      Remote Operations
      • EcoStruxure Geo SCADA Expert Forum
      • Remote Operations Forum
      Industrial Automation
      • Alliance System Integrators Forum
      • AVEVA Plant SCADA Forum
      • CPG Expert Forum DACH
      • EcoStruxure Automation Expert / IEC 61499 Forum
      • Fabrika ve Makina Otomasyonu Çözümleri
      • Harmony Control Customization Forum
      • Industrial Edge Computing Forum
      • Industry Automation and Control Forum
      • Korea Industrial Automation Forum
      • Machine Automation Forum
      • Modicon PAC Forum
      • PLC Club Indonesia
      Schneider Electric Wiser
      • Schneider Electric Wiser Forum
      Power Distribution IEC
      • Eldistribution & Fastighetsautomation
      • Elektrik Tasarım Dağıtım ve Uygulama Çözümleri
      • Paneelbouw & Energie Distributie
      • Power Distribution and Digital
      • Solutions for Motor Management
      • Specifiers Club ZA Forum
      • Електропроектанти България
      Power Distribution NEMA
      • Power Monitoring and Energy Automation NAM
      Power Distribution Software
      • EcoStruxure Power Design Forum
      • LayoutFAST User Group Forum
      Energy & Sustainability Services
      • Green Building Scoring and Certification Forum
      Light and Room Control
      • SpaceLogic C-Bus Forum
      Solutions for your Business
      • Solutions for your Business Forum
      Support
      • Ask the Community
  • Knowledge Center
    • Building Automation Knowledge Base
    • Remote Operations Devices Knowledge Base
    • Geo SCADA Knowledge Base
    • Industrial Automation How-to videos
    • Digital E-books
    • Success Stories Corner
  • Events & Webinars
    • All Events
    • Innovation Talks
    • Innovation Summit
    • Let's Exchange Series
    • Partner Success
    • Process Automation Talks
    • Technology Partners
  • Ideas
    • EcoStruxure Building
      • EcoStruxure Building Advisor Ideas
      Remote Operations
      • EcoStruxure Geo SCADA Expert Ideas
      • Remote Operations Devices Ideas
      Industrial Automation
      • Modicon Ideas & new features
  • Blogs
    • By Topic
    • By Topic
      EcoStruxure Power & Grid
      • Backstage Access Resources
      EcoStruxure IT
      • EcoStruxure IT™ Advisor CFD
      Remote Operations
      • Remote Operations Blog
      Industrial Automation
      • Industrie du Futur France
      • Industry 4.0 Blog
      Power Distribution NEMA
      • NEMA Power Foundations Blog
      Energy & Sustainability Services
      • Active Energy Management Blog
      Light and Room Control
      • KNX Blog
      Knowledge Center
      • Digital E-books
      • Geo SCADA Knowledge Base
      • Industrial Automation How-to videos
      • Remote Operations Devices Knowledge Base
      • Success Stories Corner
  • companyImpact

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
  • Communities
  • 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 Exchange 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
  • Lists, Events & Alarms 21
  • WebX 19
  • Request Form 18
  • ViewX 15
  • Setup 12
  • Application Programming 12
  • Telemetry 7
  • Mimic Graphics 7
  • Downloads 6
  • Support 5
  • IoT 5
  • Drivers and Communications 4
  • security 4
  • SCADA 4
  • Geo SCADA Expert 4
  • DNP 3 3
  • IEC 61131-3 Logic 3
  • Events & Alarms 2
  • Trends and Historian 2
  • Virtual ViewX 2
  • Lists 2
  • Privacy Policy 1
  • OPC-UA 1
  • Architectures 1
  • Templates and Instances 1
  • ClearSCADA 1
  • Releases 1
  • Maps and GIS 1
  • Tools & Resources 1
  • Mobile 1
  • Geo Scada 1
  • Previous
  • 1 of 4
  • Next
Latest Blog Posts
  • Geo SCADA Expert 2022
  • Geo SCADA 2022 Operating System Support
  • Geo SCADA 2022 Upgrade Strategy
  • Geo SCADA 2022 Upgrade Notes
  • Geo SCADA 2022 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
sbeadle1
Spock sbeadle1 Spock
Spock
‎2021-06-09 04:02 PM
0 Likes
0
878
  • Bookmark
  • Subscribe
  • Email to a Friend
  • Printer Friendly Page
  • Report Inappropriate Content
‎2021-06-09 04:02 PM

Accessing Data via SQL in Structured Text

Originally published on Geo SCADA Knowledge Base by sbeadle1 | 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. 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

sbeadle1

  • Back to Stream
  • 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 forum after you log in or create your free account.
Forum-Icon

Create your free account or log in to subscribe to the forum - 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 © 2023 Schneider Electric, Inc