Variables in Stored Programs
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 , "Local VariableDECLARE
Syntax". - Variables can be set directly with the
SET
statement. See , "SET
Syntax". - Results from queries can be retrieved into local variables using
SELECT ... INTO
or by opening a cursor and usingvar_list
FETCH ... INTO
. See , "var_list
SELECT ... INTO
Syntax", and , "Cursors".
For information about the scope of local variables and how MariaDB resolves ambiguous names, see , "Local Variable Scope and Resolution".