SQLCommand Data Object

Help Contents


An SQLCommand Data Object represents an SQL Command on a data source. The SQLCommand Data Object can represent:

Creating an SQLCommand Data Object

How you create an SQLCommand Data Object depends on where the iEC 61131-3 code is running:

SQLCommand Functions

Once created, use these functions to access an SQLCommand Data Object:

SqlCommandColumnCount Returns the number of columns in an SQLCommand data object.
SqlCommandFetchRow Fetches the next row in an SQLCommand data object.
SqlCommandGetValue Returns the value of a column in the current row if an SQLCommand data object.
SqlCommandIsNullValue Returns whether a column is null in the current row of an SQLCommand data object.
SqlCommandRowCount Returns the number of rows in an SQLCommand data object.


  1. Code for an Operator Action showing the Number of Tags in the database:
    VAR CommandId, Count : DINT; END_VAR
    (* Create the SQLCommand Data Object *)
    CommandId := SqlCommandFromTagDB('SELECT COUNT(*) FROM TagCore');
    (* Fetch the first row and if successful... *)
    IF SqlCommandFetchRow( CommandId ) THEN
        (* Get the count *)
        Count := TO_DINT( SqlCommandGetValue( CommandId, 0 ) );
        (* Display the count *)
            'Tag Count', 
            CONCAT( 'Tag Count = ', TO_STRING( Count ) ) );
  2. Code for a Program Tag writing the number of tags in the database to another tag:
    Program Main
            Timer : TON;
            SqlCmd : SqlCommandFromTagDB;
            Count AT %".Tag Count" : WORD;
        (* Run the TON function block *)
        Timer( IN:=NOT Timer.Q, PT:=T#30s );
        (* Run the SqlCommandFromTagDB block when the timer completes *)
            Req:= Timer.Q,
            SQL:='SELECT COUNT(*) FROM TagCore' );
        (* If the SQL command has completed... *)
        IF SqlCmd.DONE THEN
            (* Fetch the first row *)
            IF SqlCommandFetchRow( SqlCmd.CommandId ) THEN
                (* Get the value and write to the tag *)
                Count := TO_WORD( SqlCommandGetValue( SqlCmd.CommandId, 0 ) );	

Further Information

SQL Language

To learn about the SQL Language supported in Fernhill SCADA.

Data Objects

To learn how to use data objects in IEC 61131-3 code.

IEC 61131-3

To learn how IEC 61131-3 is used in Fernhill SCADA.

Operator Action Editor

To learn about editing Operator Actions.


For the meaning of terms used in Fernhill SCADA.