Prepare-execute
Insert new rows matching the table schema.
PREPARE
-
Prepare a statement for later use. The statement can include parameters to be replaced at execution time.
-
Parameters are represented by question marks (?). When using parameters, use with the EXECUTE statement.
PREPARE statement_name FROM statement
statement_name
: The name to identify the statement.statement
: The query statement.
EXECUTE
-
Execute a statement prepared with PREPARE. If parameters were used in PREPARE, define values with USING.
PREPARE statement_name FROM statement
statement_name
: The name to identify the statement.
EXECUTE IMMEDIATE
-
Execute a statement immediately without using PREPARE. If parameters are used, define values with USING.
EXECUTE IMMEDIATE `statement` [ USING parameter1 [ , parameter2, ... ] ]
statement
: The statement to execute.
DEALLOCATE PREPARE
-
Delete a statement defined with PREPARE.
DEALLOCATE PREPARE my_query;
Example
PREPARE-EXECUTE statement example
prepare my_select1 from
select name from nation;
execute my_select1;
-- when using parameters
PREPARE my_select2 FROM
SELECT name FROM nation WHERE regionkey = ? and nationkey < ?;
EXECUTE my_select2 USING 1, 3;
EXECUTE IMMEDIATE
'SELECT name FROM nation';
EXECUTE IMMEDIATE
'SELECT name FROM nation WHERE regionkey = ? and nationkey < ?'
USING 1, 3;
DEALLOCATE PREPARE my_select1;