§ PREPARE Statements
This section describes how to create, use, and drop PREPARE prepared statements in the GreatSQL database.
In GreatSQL, prepared statements (also called parameterized queries or precompiled statements) are used to execute the same or similar SQL statements multiple times with different parameters. Prepared statements can improve performance because the database can optimize and reuse the query plan, and they also help prevent SQL injection attacks.
§ Creating a Prepared Statement
PREPARE stmt_name FROM preparable_stmt;
- stmt_name: the name of the prepared statement.
- preparable_stmt: the SQL statement to be prepared.
§ Executing a Prepared Statement
Before executing a prepared statement, you need to set the variables with the SET statement.
SET @{parameter_name} = {parameter_value};
- parameter_name: the variable name.
- parameter_value: the variable value.
After the variables are set, execute the statement with the EXECUTE statement.
EXECUTE stmt_name [USING @var_name [, @var_name] ...];
- stmt_name: the name of the prepared statement.
- @var_name: the variable name.
§ Dropping a Prepared Statement
DEALLOCATE PREPARE stmt_name;
- stmt_name: the name of the prepared statement.
§ Examples
§ Query Example
Create a user table and insert three rows.
CREATE TABLE user (id INT NOT NULL AUTO_INCREMENT, name VARCHAR(20) DEFAULT NULL, PRIMARY KEY (id));
INSERT INTO user VALUES (1, '刘一');
INSERT INTO user VALUES (2, '陈二');
INSERT INTO user VALUES (3, '张三');
2
3
4
Create a prepared statement named stmt1, using a question mark (?) as the parameter placeholder in the SQL statement.
greatsql> PREPARE stmt1 FROM 'SELECT id, name FROM user WHERE id = ?';
Query OK, 0 rows affected (0.01 sec)
Statement prepared
2
3
Set the variable with SET:
greatsql> SET @id = 1;
Query OK, 0 rows affected (0.01 sec)
2
Execute the prepared statement:
greatsql> EXECUTE stmt1 USING @id;
+----+--------+
| id | name |
+----+--------+
| 1 | 刘一 |
+----+--------+
1 row in set (0.00 sec)
2
3
4
5
6
7
§ Insert Example
Using the user table as an example, insert a row with id 4 and name 李四. Because the id primary key field of the user table has the AUTO_RANDOM attribute, the id value does not need to be specified when inserting.
greatsql> PREPARE `user_insert` FROM 'INSERT INTO `user` (name) VALUES (?);';
Query OK, 0 rows affected (0.02 sec)
Statement prepared
2
3
Set the variable:
greatsql> SET @name = '李四';
Query OK, 0 rows affected (0.00 sec)
2
Execute the prepared statement:
greatsql> EXECUTE `user_insert` USING @name;
Query OK, 1 row affected (0.01 sec)
2
View the data in the table:
greatsql> SELECT * FROM user;
+----+--------+
| id | name |
+----+--------+
| 1 | 刘一 |
| 2 | 陈二 |
| 3 | 张三 |
| 4 | 李四 |
+----+--------+
4 rows in set (0.01 sec)
2
3
4
5
6
7
8
9
10
Scan the QR code to follow our WeChat Official Account
