DATABASE statement

Writes data to an external relational database.

Syntax

exec_clause select_clause from_clause [matching_clause] [on_clause] [where_clause] [group_by_clause] [having_clause] [order_by_clause] [limit_clause] [output_clause] ;
Components

exec_clause

The destination for the published rows. See EXECUTE STATEMENT DATABASE Clause for more information. The statements in this clause are executed on the external database whenever this statement generates output.

select_clause

The select list specifying what to publish. See SELECT Clause for more information. All non-asterisk (*) items in this list must include an AS subclause.

from_clause

The data sources. See FROM Clause for more information.

matching_clause

A pattern-matching specification. See MATCHING Clause for more information.

on_clause

A join condition. See ON Clause for more information.

where_clause

A selection condition. See WHERE Clause for more information.

group_by_clause

A partitioning specification. See GROUP BY Clause for more information.

having_clause

A filter definition. See HAVING Clause for more information.

order_by_clause

A sequencing definition. See ORDER BY Clause for more information.

limit_clause

A limit on the number of rows to publish. See LIMIT Clause for more information.

output_clause

A synchronization specification. See OUTPUT Clause for more information.

Usage

The Database statement is a CCL statement that directly modifies data in an external relational database. You must have previously configured the database, as described in the Sybase CEP Installation Guide . Enter the Database statement directly into the CCL query module, and it executes SQL statements against the external database without going through an output adapter. In the case of kdb+ databases, the Database statement can also contain statements in Kx System's language q.

This statement is used only to pass SQL or q statements to databases, not to retrieve data from databases. Data retrieval from databases is handled by database subqueries (see Database Subquery for more information).

The Database statement is syntactically similar to the Query statement. It contains a number of clauses that create a query that subscribes to rows from a data stream, processes the incoming rows, and perform actions against the external database based on the results.

In addition to using the Database statement, you can also write to databases using the Write to DB output adapter, which passes SQL statements to an external database. For instructions on using the adapter, see the Sybase CEP Integration Guide .

Clause Processing Order

The main clauses within a Database statement are processed in the following order, which affects the query's output:

  1. FROM clause (with MATCHING conditions, if any).

  2. WHERE clause (with MATCHING conditions, if any).

  3. SELECT clause (in conjunction with GROUP BY, if present).

  4. HAVING clause.

  5. OUTPUT clause.

  6. ORDER BY clause.

  7. LIMIT clause.

  8. INSERT clause.

Restrictions

See Also

Examples

The following simple example of a Database statement deletes rows from database table Table1, based on their match to values in the Column1 column of StreamIn.

EXECUTE STATEMENT DATABASE "ExternalDatabase"
  [[DELETE FROM Table1 WHERE Table1.Column1=?Column1]]
SELECT StreamIn.Column1 AS Column1
FROM StreamIn;

The following 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;