PREPARE prepares a statement for execution
EXECUTE executes a prepared statement
DEALLOCATE PREPARE releases a prepared statement
SET @s = 'SELECT SQRT(POW(?,2) + POW(?,2)) AS hypotenuse';
PREPARE stmt2 FROM @s;
SET @a = 6;
SET @b = 8;
EXECUTE stmt2 USING @a, @b;
Result:
+------------+
| hypotenuse |
+------------+
| 10 |
+------------+
Finally,
DEALLOCATE PREPARE stmt2;
Notes:
FROM @s
(This is a request for a good example that shows how to construct a SELECT
using CONCAT
, then prepare+execute it. Please emphasize the use of @variables versus DECLAREd variables -- it makes a big difference, and it is something that novices (include myself) stumble over.)
SET v_column_definition := CONCAT(
v_column_name
,' ',v_column_type
,' ',v_column_options
);
SET @stmt := CONCAT('ALTER TABLE ADD COLUMN ', v_column_definition);
PREPARE stmt FROM @stmt;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;