docs/en/sql-reference/sql-statements/prepared_statement.md
From v3.2 onwards, StarRocks provides prepared statements for executing SQL statements multiple times with the same structure but different variables. This feature significantly improves execution efficiency and prevents SQL injection.
The prepared statements basically work as follows:
?. The FE parses the SQL statement and generates an execution plan.Benefits
Usages
Prepared statements are effective only in the current session and cannot be used in other sessions. After the current session exits, the prepared statements created in that session are automatically dropped.
The execution of a prepared statement consists of the following phases:
?.Syntax:
PREPARE <stmt_name> FROM <preparable_stmt>
Parameters:
stmt_name: the name given to the prepared statement, which is subsequently used to execute or deallocate that prepared statement. The name must be unique within a single session.preparable_stmt: the SQL statement to be prepared, where the placeholder for variables is a question mark (?). Currently, only the SELECT statement is supported.Example:
Prepare a SELECT statement with specific values represented by placeholders ?.
PREPARE select_by_id_stmt FROM 'SELECT * FROM users WHERE id = ?';
Syntax:
SET @var_name = expr [, ...];
Parameters:
var_name: the name of a user-defined variable.expr: a user-defined variable.Example: Declare variables.
SET @id1 = 1, @id2 = 2;
For more information, see user-defined variables.
Syntax:
EXECUTE <stmt_name> [USING @var_name [, @var_name] ...]
Parameters:
var_name: the name of a variable declared in the SET statement.stmt_name: the name of the prepared statement.Example:
Pass variables to a SELECT statement and execute that statement.
EXECUTE select_by_id_stmt USING @id1;
Syntax:
{DEALLOCATE | DROP} PREPARE <stmt_name>
Parameters:
stmt_name: The name of the prepared statement.Example:
Delete a prepared statement.
DROP PREPARE select_by_id_stmt;
The following example demonstrates how to use prepared statements to insert, delete, update, and query data from a StarRocks table:
Assuming the following database named demo and table named users are already created:
CREATE DATABASE IF NOT EXISTS demo;
USE demo;
CREATE TABLE users (
id BIGINT NOT NULL,
country STRING,
city STRING,
revenue BIGINT
)
PRIMARY KEY (id)
DISTRIBUTED BY HASH(id);
Prepare statements for execution.
PREPARE select_all_stmt FROM 'SELECT * FROM users';
PREPARE select_by_id_stmt FROM 'SELECT * FROM users WHERE id = ?';
Declare variables in these statements.
SET @id1 = 1, @id2 = 2;
Use the declared variables to execute the statements.
-- Query all data from the table.
EXECUTE select_all_stmt;
-- Query data with ID 1 or 2 separately.
EXECUTE select_by_id_stmt USING @id1;
EXECUTE select_by_id_stmt USING @id2;
The following example demonstrates how a Java application can use a JDBC driver to insert, delete, update, and query data from a StarRocks table:
When specifying StarRocks' connection URL in JDBC, you need to enable server-side prepared statements:
jdbc:mysql://<fe_ip>:<fe_query_port>/useServerPrepStmts=true
The StarRocks GitHub project provides a Java code example that explains how to insert, delete, update, and query data from a StarRocks table through a JDBC driver.