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
来定义局部变量,存储例程(过程和函数)可以声明为获取在例程及其调用方之间传递值的参数。
To declare local variables, use the 要声明局部变量,请使用DECLARE
statement, as described in Section 13.6.4.1, “Local Variable DECLARE Statement”.DECLARE
语句,如第13.6.4.1节,“局部变量声明语句”所述。
Variables can be set directly with the 变量可以直接用SET
statement. SET
语句设置。See Section 13.7.6.1, “SET Syntax for Variable Assignment”.请参阅第13.7.6.1节,“变量赋值的设置语法”。
Results from queries can be retrieved into local variables using 查询的结果可以使用SELECT ... INTO
or by opening a cursor and using var_list
FETCH ... INTO
. var_list
SELECT ... INTO
检索到局部变量中或者打开一个指针并使用var_list
FETCH ... INTO
。var_list
See Section 13.2.10.1, “SELECT ... INTO Statement”, and Section 13.6.6, “Cursors”.请参阅第13.2.10.1节,“SELECT ... INTO”语句和第13.6.6节,“指针”。
For information about the scope of local variables and how MySQL resolves ambiguous names, see Section 13.6.4.2, “Local Variable Scope and Resolution”.有关局部变量的作用域以及MySQL如何解析不明确名称的信息,请参阅第13.6.4.2节,“局部变量作用域和解析”。
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). var_name
= DEFAULTDEFAULT
赋给存储过程或函数参数或存储程序局部变量(例如,使用SET
语句)。var_name
= DEFAULTIn MySQL 8.0, this results in a syntax error.在MySQL 8.0中,这会导致语法错误。