Documentation

Home > Documentation > TRUNCATE Statement

TRUNCATE Statement

Syntax

TRUNCATE table_identifier USING {stream_identifier | stream_expression}
  [RETURNING target_list]

Substitutable Fields

table_identifier:

The unique identifier (name) of the table to truncate.

stream_identifier:

The unique identifier (name) of the stream that, when a tuple is present, will cause truncation of the table.

stream_expression:

A StreamSQL statement that produces a stream. The statement must be enclosed within parentheses.

target_list:

One or more entries, separated by commas, of the format target_list_entry.

target_list_entry:

A value, of the format scalar_expression [AS output_field_identifier], to be included in the result set returned by the statement.

scalar_expression:

An expression that generates a value for the tuple that is returned by the truncate operation. Values may be obtained from a tuple field or from a simple function. Optionally, the name for a value may be modified through an AS clause.

Discussion

The TRUNCATE statement completely empties, but does not delete, a table.

With the TRUNCATE statement, the USING clause identifies the associated stream. The actual content of the tuples contained within this stream are not used in executing the stream query or query statement. Just the presence of a tuple on this stream causes the query or statement to execute, which deletes the entire contents of the table but leaves the table itself.

In the RETURNING clause, scalar_expression may specify a value from a function, the stream and/or from the table. However, since the contents of the table have been deleted, any value derived from the table will be null. Therefore, it only makes sense to include function or tuple derived values in the RETURNING clause. For example:

TRUNCATE ...
  RETURNING tuple_field_identifier [AS ...][, ...]

The result set generated by the RETURNING clause must be captured into a stream. You may use the CREATE STREAM statement to define a stream and the INTO keyword to populate the stream with the content generated by the RETURNING clause. Alternatively, in a single statement you may use the => (arrow) operator with a CREATE STREAM statement, as illustrated below.

CREATE STREAM stream_identifier;
TRUNCATE ... RETURNING ... INTO stream_identifier;

Or

TRUNCATE ... RETURNING ... => CREATE STREAM stream_identifier