EVAL statement

The EVAL statement takes a character value, interprets it as an SQL statement, and processes that statement.

For details of the EVAL function, see EVAL function.

Syntax

Read syntax diagramSkip visual syntax diagramEVAL( SQL_character_value )

EVAL takes one parameter in the form of an expression, evaluates this expression, and casts the resulting value to a character string if it is not one already. The expression that is passed to EVAL must therefore be able to be represented as a character string.

After this first stage evaluation is complete, the behavior of EVAL depends on whether it is being used as a complete ESQL statement, or in place of an expression that forms part of an ESQL statement:
  • If it is a complete ESQL statement, the character string derived from the first stage evaluation is processed as if it were an ESQL statement.
  • If it is an expression that forms part of an ESQL statement, the character string is evaluated as if it were an ESQL expression and EVAL returns the result.

User defined procedures cannot be defined within an EVAL statement but EVAL can be used to call a user-defined procedure that is in scope where the EVAL statement is used.

If you use the EVAL statement to call out to a user-defined procedure that is not called from anywhere else in the ESQL for a given node, you need to add the following code to your ESQL, to ensure that the user-defined procedure being called is included when the code is compiled:
   IF (FALSE) THEN CALL procedure(<parameters>); END IF;
Note, that in the preceding code, you must replace procedure() with the named procedure in question.

In the following examples, A and B are integer scalar variables, and scalarVar1 and OperatorAsString are character string scalar variables.

The following examples are valid uses of EVAL:
  • SET OutputRoot.XMLNS.Data.Result = EVAL(A+B);

    The expression A+B is acceptable because, although it returns an integer value, integer values are representable as character strings, and the necessary cast is performed before EVAL continues with its second stage of evaluation.

  • SET OutputRoot.XMLNS.Data.Result = EVAL('A' || operatorAsString || 'B');
  • EVAL('SET ' || scalarVar1 || ' = 2;');

    The semicolon included at the end of the final string literal is necessary, because if EVAL is being used in place of an ESQL statement, its first stage evaluation must return a string that represents a valid ESQL statement, including the terminating semicolon.

Variables declared in an EVAL statement do not exist outside that EVAL statement.

The real power of EVAL is that it allows you to dynamically construct ESQL statements or expressions. In the second and third examples above, the value of scalarVar1 or operatorAsString can be set according to the value of an incoming message field, or other dynamic value, allowing you to effectively control what ESQL is processed without requiring a potentially lengthy IF-THEN ladder.

However, consider the performance implications in using EVAL. Dynamic construction and processing of statements or expressions is necessarily more time-consuming than simply processing pre-constructed ones. If performance is vital, you might prefer to write more specific, but faster, ESQL.

The following are not valid uses of EVAL:
  • SET EVAL(scalarVar1) = 2;

    In this example, EVAL is being used to replace a field reference, not an expression.

  • SET OutputRoot.XMLNS.Data.Result[] = EVAL((SELECT T.x FROM Database.y AS T));

    In this example, the (SELECT T.x FROM Database.y) passed to EVAL returns a list, which is not representable as a character string.

The following example is acceptable because (SELECT T.x FROM Database.y AS T) is a character string literal, not an expression in itself, and therefore is representable as a character string.
SET OutputRoot.XMLNS.Data.Result[]
 = EVAL('(SELECT T.x FROM Database.y AS T)');

Functions that are referenced only in an EVAL statement, and not in the rest of the ESQL module, might not be included in the BAR file. In the following examples, the function MyFunction must be referenced somewhere else in the ESQL module, otherwise the BAR file might fail to deploy.

EVAL('CALL MyFunction(parm1, parm2);');
DECLARE functionName CHARACTER 'Function';
DECLARE callStmt CHARACTER 'CALL My' || functionName || '(parm1, parm2);';
EVAL(callStmt);