CQL supports prepared statements. Using a prepared statement, Cassandra parses a query only once, but executes it multiple times with different concrete values.
In a statement, each time a column value is expected in the data manipulation and query statements, a bind variable marker can be used instead. A statement with bind variables must then be prepared. During execution of the prepared statement, concrete values for bind variables are provided in the order the bind variables are defined in the query string.¶
- INSERT statement values
INSERT INTO playlists (id, song_order, song_id, title, artist, album) VALUES (62c36092-82a1-3a00-93d1-46196ee77204, 1, a3e64f8f-bd44-4f28-b8d9-6938726e34d4, ?, ?, ?);
- Values on the right side of the = operator in the SET and clauses of the UPDATE statement
UPDATE users SET name = ?, email = ? WHERE user_uuid = ?;
- Values on the right side of the IN or = operators in the WHERE clause of the DELETE statement
DELETE email, phone FROM users USING TIMESTAMP 1318452291034 WHERE user_name = '?';
- Values for the TIMESTAMP and TTL properties of the UPDATE statement
UPDATE myTable USING TTL ? SET v = 2 WHERE k = 'foo';
- Value of the LIMIT property in a SELECT statement
SELECT * FROM myTable LIMIT ?;
The exact procedure to prepare a statement and execute a prepared statement depends on the CQL driver used and is beyond the scope of this document. The Java Driver documentation includes an example.
Cassandra 2.0 and later support a query that uses bind variables even though the statement is not prepared. In Cassandra 2.0.1 (CQL 3.1.1) and later, you can use named bind variables (:name instead of ?).