System variables and user-defined variables can be used in stored
programs, just as they can be used outside stored-program context.
In addition, stored programs can use DECLARE
to
define local variables, and stored routines (procedures and
functions) can be declared to take parameters that communicate
values between the routine and its caller.
To declare local variables, use the
DECLARE
statement, as described in Section 15.6.4.1, βLocal Variable DECLARE Statementβ.Variables can be set directly with the
SET
statement. See Section 15.7.6.1, βSET Syntax for Variable Assignmentβ.Results from queries can be retrieved into local variables using
SELECT ... INTO
or by opening a cursor and usingvar_list
FETCH ... INTO
. See Section 15.2.13.1, βSELECT ... INTO Statementβ, and Section 15.6.6, βCursorsβ.var_list
For information about the scope of local variables and how MySQL resolves ambiguous names, see Section 15.6.4.2, βLocal Variable Scope and Resolutionβ.
It is not permitted to assign the value DEFAULT
to stored procedure or function parameters or stored program local
variables (for example with a SET
statement). In MySQL 8.0, this results in a syntax
error.
var_name
= DEFAULT