系统变量以及用户自定义变量可以在存储程序中使用,就像它们也可以在存储程序上下文之外使用一样。此外,存储程序可以使用 DECLARE 来定义本地变量,存储例程(过程和函数)也可以声明,以获取在例程与调用者之间传递数值的参数。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.
要声明局部变量,可以使用
DECLARE语句,如 第 15.6.4.1 节,“局部变量 DECLARE Statement”。To declare local variables, use theDECLAREstatement, as described in Section 15.6.4.1, “Local Variable DECLARE Statement”.变量可以直接设置
SET语句。参见 第 15.7.6.1 节,“SET Syntax for Variable Assignment”。Variables can be set directly with theSETstatement. See Section 15.7.6.1, “SET Syntax for Variable Assignment”.Results from queries can be retrieved into local variables using
SELECT ... INTOor by opening a cursor and usingvar_listFETCH ... INTO。参见Section 15.2.13.1, “SELECT ... INTO Statement”,以及Section 15.6.6, “Cursors”.Results from queries can be retrieved into local variables usingvar_listSELECT ... INTOor by opening a cursor and usingvar_listFETCH ... INTO. See Section 15.2.13.1, “SELECT ... INTO Statement”, and Section 15.6.6, “Cursors”.var_list
有关局部变量的作用域及 MySQL 如何解决歧义名称的信息,请参见 第 15.6.4.2 节,”局部变量作用域与解析“。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”.
不允许将 DEFAULT给存储过程、函数参数或存储程序的本地变量(例如 SET
var_name = DEFAULT 语句)。在 MySQL 8.4 中,这会导致语法错误。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.4, this results in a syntax
error.var_name = DEFAULT