Chapter 7 Working With the Query Language : Using Bind Variables in Query Text

Using Bind Variables in Query Text
Query definitions can use literal values for entity attributes in query text, or they can use bind variables whose values are provided at runtime.
In the Query.create() function, use a dollar sign ($) to indicate a bind variable in the query text. (See $min in the example below.)
The values for all bind variables must be supplied to a statement when it executes. Set the value of a bind variable, using the Query.Statement.setVar() function, from the CEP Query Functions catalog, as shown next.

 
Query.Statement.setVar(String StatementName, String BindVariableName, Object value);

 
When you use the Query.Statement.setVar() function, functions must be called in the following order:
Query.Statement.open()
Query.Statement.setVar()
Query.Statement.execute() OR Query.Statement.executeWithCallback()
All three functions must reference the same query statement name.
The following example shows how a bind variable in a query definition is set as the value of an event property by the Query.Statement.setVar() function. The value could be defined as a literal value as desired, or in some other way, depending on context and need.
Example

 
Query.create("report927", "select agent_name, total_sales, zipcode from /Concepts/Sales where total_sales >= $min");
Query.Statement.open("report927", S_Id);
Query.Statement.setVar(S_Id, "min", evt.min_total_sales);
Query.Statement.execute(S_Id, "rset");

 
Where evt.min_total_sales is an event property of a numeric type.
Clearing Bind Variables
You can use Query.Statement.clearVars() to clear all bind variable values associated with the named statement.