Prepared Statement
A prepared statement is used to execute the same statement repeatedly with embedded values.
Usage Flow
- Prepare a statement.
- Execute the statement using embedded values.
- Dispose the statement as necessary.
Prepare Statement
PREPARE statement_name FROM statement;
- statement_name
- identifier
- statement
- string
Execute Prepared Statement
execute_statement
: EXECUTE statement_name;
| EXECUTE statement_name USING replace_value [, replace_value ...];
replace_value
: value
| value AS placeholder_name
- statement_name
- identifier
- value
- value
- placeholder_name
- identifier
Placeholder names are used for named placeholders.
Dispose Prepared Statement
DISPOSE PREPARE statement_name;
- statement_name
- identifier
Placeholder
- Positional Placeholder
- Question Mark(U+003F
?
) - Named Placeholder
- Colon(U+003A
:
) and followd by identifier
Example
-- Positional Placeholder
PREPARE stmt1 FROM 'SELECT ?, ?, ?;';
EXECUTE stmt1 USING 'a', 'b', 'c';
-- Named Placeholder
PREPARE stmt2 FROM 'SELECT :second, :third, :first;';
EXECUTE stmt2 USING 'a' AS `first`, 'b' AS `second`, 'c' AS `third`;