EXECUTE STATEMENT DATABASE clause

Specifies the name of an external relational database connection and the SQL or q statements that should be executed against the database.

Syntax

EXECUTE STATEMENT DATABASE "service" [[statements]]   
   SELECT…
Components

service

The name of a database service defined in the file c8-services.xml. For more information about configuring Sybase CEP Engine database services, see the Sybase CEP Installation Guide .

statements

The statements to be executed by the database software.

Usage

The EXECUTE STATEMENT DATABASE clause is the first clause of the Database statement. It indicates the database that is impacted when the CCL query in the Database statement generates output, and specifies the SQL or q statements that should be executed against the database in that event.

Sybase CEP Engine sends the text you specify (as statements) to the external database for execution after replacing references to CCL columns and parameters with the appropriate values. Since Sybase CEP Engine does not interpret the statements beyond replacing the column and parameter references, you may specify any statement supported by your database. However, Sybase CEP Engine ignores any return values. To retrieve data from an external database, use a database subquery.

To include the value of a CCL column in your database statement, preface the column name with a question mark (?stream_or_alias.column). To include the value of a CCL parameter, preface the parameter name by a question mark and a dollar sign (?$parameter).

ON ERROR extension

Use the ON ERROR extension to handle database errors that may cause Sybase CEP Engine to stop project execution.

The syntax for the EXECUTE STATEMENT DATABASE clause with the ON ERROR extention is:
EXECUTE STATEMENT DATABASE "service" [[statements]]   
   ON ERROR [error_insert_clause] CONTINUE
   SELECT…
Additional syntax related to the error_insert_clause:
error_insert_clause: INSERT INTO error_stream_name error_select_list
error_stream_name: Name of an output stream or local stream.
error_select_list: SELECT {error_expression} [,...]
error_expression: A CCL expression.
The error_expression can only contain:
  • References to columns in the stream specified in the stream_clause.
  • Operators, constant literals, and scalar functions.
  • CCL parameters in the project. These should be prefaced with $.
  • The ERROR_MESSAGE() built in.

The ON ERROR extension is optional. The error_insert_clause for the ON ERROR extension is also optional. If the ON ERROR extension is not provided and errors occur, the behavior of this clause is determined by the setting of the IgnoreErrors property in the c8-services.xml service configuration file. By default, this property is set to 'false', which means that if an error occurs, the query is aborted.

Only the INSERT INTO statement or the EXECUTE STATEMENT DATABASE clauses are legal within the ON ERROR clause.

When the ON ERROR extension is provided without the error_insert_clause, errors found in the execution of this clause are ignored and CEP Engine continues to execute the project. Subsequent tuples arriving in any of the input streams of the from_clause continue to trigger execution of this clause. If these executions generate errors, they are also ignored by the CEP Engine.

When the ON ERROR extension is provided with the error_insert_clause, errors found in the execution of this clause are ignored and CEP Engine continues to execute the project. However a new tuple is also inserted into the errorstream (specified by error_stream_name). This tuple contains the fields selected in the error_select_list clause. The timestamp of this tuple is the same as the timestamp of the tuple in the from_clause that triggered the execution of this clause. These tuples are inserted in the errorstream in the same order as that of the original tuples in the from_clause that triggered the execution of this clause.

The ON ERROR extension cannot be used with both an EXECUTE STATEMENT DATABASE/EXECUTE REMOTE PROCEDURE clause and a REMOTE SUBQUERY/PROCEDURE clause in the same CCL statement. This causes errors. To avoid errors, the CCL statements must be broken into separate statements using an intermediate local stream.

Set the MaxRetries parameter in the service definitions for Database to determine how many times Sybase CEP Engine should retry statement execution after experiencing errors. The parameter in the service file is written as follows:
<Param Name="MaxRetries">some_number</Param>
where some_number is an integer between 0 and 255.

If the MaxRetries parameter is set to a value greater than 0, say N, then every time there is an error in an Execute Statement or Subquery statement that subscribes to this service definition, the Sybase CEP Engine automatically retries the execution of that statement up to a maximum of N number of times. If the execution of the statement fails for all the N number of times, an error tuple is inserted into the errorstream. The default value for MaxRetries is 0, therefore there is no retry.

Kdb+ and q

EXECUTE STATEMENT DATABASE clauses that modify kdb+ databases can contain statements in q language.

Important:

The q statement cannot contain newline characters. Everything between the brackets ([[ ]]) must be entered on a single line.

When you pass values from the Database statement's data source to the q statements in the EXECUTE STATEMENT DATABASE clause, you must enter the values as a space-separated list that appears immediately after the parameterized q statement. Sybase CEP Engine passed the values to the final q statement as a single general (multi-type) list. This list takes the place of the right side of the q statement (the part of the statement after the operator). For example, consider the following q statement:
5+(1;2;3)
To pass column values for the three parameters, you would write it like this:
5+ ?MyStream.IntColumn1 ?MyStream.IntColumn2 ?MyStream.IntColumn3
If you pass a list of parameters to the q (or Q-SQL) statement, the final statement must be written in the brackets to accommodate the parameters. For example:
{[params] select Price, Volume from trades where StockSym = `$params[0],
   Volume > `$params[1]} ?ParamStream.StockSym ?ParamStream.Volume
Sybase CEP Engine automatically converts CCL datatypes to a limited set of q datatypes. To convert a CCL datatype to another kdb+ type, you must explicitly cast the CCL type as the desired kdb+ type in your q statement. For example, the following Database statement casts the values from four columns to datetime, symbol, real, and short in q:
EXECUTE STATEMENT DATABASE "MyKDBService"
[[`trades insert (`datetime ; ` ; `real ; `short) $ ?Ts ?StockSym ?Price ?Volume]]
SELECT GETTIMESTAMP (QueryIn) AS Ts,
       StockSym AS StockSym,
       Price AS Price,
       Volume AS Volume
FROM QueryIn;
Note: Specify all types for all of your parameters, even to cast a single value.

Examples

This query updates a table called MyTable in a database called MyDatabase. The Symbol and Price columns (aliased as CurSymbol and CurPrice) are selected from a Sybase CEP stream called StreamIn. The SQL query matches symbols in the Symbol column of MyTable with those of the Symbol column in StreamIn, and records the corresponding price from the Price column in StreamIn to a column called Price in MyTable.
EXECUTE STATEMENT DATABASE "MyDatabase"
  [[UPDATE MyTable 
    SET MyTable.Price = ?CurPrice 
    WHERE MyTable.Symbol=?CurSymbol]]
SELECT
  StreamIn.Symbol AS CurSymbol, StreamIn.Price AS CurPrice
FROM StreamIn;
This example inserts column values from the ActiveStrategies window and MyTrades streams, as well as a calculated timestamp into TradingDatabase:
CREATE WINDOW ActiveStrategies 
SCHEMA (Symbol STRING, Strategy STRING)
KEEP LAST PER Symbol;

CREATE INPUT STREAM MyTrades 
SCHEMA (Symbol STRING, Quantity INTEGER, Price FLOAT);

EXECUTE STATEMENT DATABASE "TradingDatabase"
   [[INSERT INTO Positions 
      VALUES (?Symbol, ?Ts, ?Quantity, ?Price, ?Strategy) ]]
SELECT *, GETTIMESTAMP(MyTrades) as Ts
FROM MyTrades, ActiveStrategies
WHERE MyTrades.Symbol = ActiveStrategies.Symbol;
This example demonstrates how errors are logged when the ON ERROR extension and insert_clause are provided with this clause:
Example:
EXECUTE STATEMENT DATABASE "StockTradeDB"
[[
    INSERT INTO TradeTable VALUE(?Symbol, ?Price, ?Volume, ?Ts)
]]
ON ERROR
INSERT INTO DBWriteErrorStream
SELECT Symbol, Ts, ERROR_MESSAGE()
CONTINUE
SELECT 
Symbol  as Symbol,
Price as Price,
Volume as Volume,
GETTIMESTAMP(InTrades) as Ts
FROM 
InTrades KEEP EVERY 1 minute;

where the schema for DBWriteErrorStream is (Symbol String, Ts TimeStamp, ErrMsg String).