8.4 中文镜像

MySQL 8.4 参考手册 · 中文镜像

正文为中文,英文原文默认隐藏。点击任意中文段落即可展开 / 收起对应英文原文。

打开官方英文原文 ↗

译文由机器翻译生成,仅供理解参考;有疑问请以官方英文原文为准。

中文点击中文段落可显示 / 隐藏英文原文本页译文覆盖 100%

27.2.1 存储过程语法27.2.1 Stored Routine Syntax

存储例程可以是过程或函数。存储例程通过 CREATE PROCEDURE 和 CREATE FUNCTION 语句创建(参见 第 15.1.17 节,“CREATE PROCEDURE 和 CREATE FUNCTION 语句”)。过程使用 CALL 语句调用(参见 第 15.2.1 节,“CALL 语句”),并且只能通过输出变量返回值。函数可以像调用任何其他函数一样在语句中调用(即通过调用函数名称),并且可以返回标量值。存储例程的主体可以使用复合语句(参见 第 15.6 节,“复合语句语法”)。A stored routine is either a procedure or a function. Stored routines are created with the CREATE PROCEDURE and CREATE FUNCTION statements (see Section 15.1.17, “CREATE PROCEDURE and CREATE FUNCTION Statements”). A procedure is invoked using a CALL statement (see Section 15.2.1, “CALL Statement”), and can only pass back values using output variables. A function can be called from inside a statement just like any other function (that is, by invoking the function's name), and can return a scalar value. The body of a stored routine can use compound statements (see Section 15.6, “Compound Statement Syntax”).

存储例程可以使用 DROP PROCEDURE 和 DROP FUNCTION 语句删除(参见 第 15.1.29 节,“DROP PROCEDURE 和 DROP FUNCTION 语句”),并可以使用 ALTER PROCEDURE 和 ALTER FUNCTION 语句修改(参见 第 15.1.7 节,“ALTER PROCEDURE 语句”)。Stored routines can be dropped with the DROP PROCEDURE and DROP FUNCTION statements (see Section 15.1.29, “DROP PROCEDURE and DROP FUNCTION Statements”), and altered with the ALTER PROCEDURE and ALTER FUNCTION statements (see Section 15.1.7, “ALTER PROCEDURE Statement”).

存储过程或函数与特定数据库相关联。这有几个影响:A stored procedure or function is associated with a particular database. This has several implications:

  • 当调用该例程时,会隐式执行 USE db_name(在例程终止时撤销)。存储例程中不允许使用 USE 语句。When the routine is invoked, an implicit USE db_name is performed (and undone when the routine terminates). USE statements within stored routines are not permitted.

  • 您可以使用数据库名称限定例程名称。这可以用来引用不在当前数据库中的例程。例如,要调用与 test 数据库相关联的存储过程 p 或函数 f,可以使用 CALL test.p() 或 test.f()。You can qualify routine names with the database name. This can be used to refer to a routine that is not in the current database. For example, to invoke a stored procedure p or function f that is associated with the test database, you can say CALL test.p() or test.f().

  • 当删除数据库时,与之关联的所有存储例程也会被删除。When a database is dropped, all stored routines associated with it are dropped as well.

存储函数不能递归。Stored functions cannot be recursive.

存储过程允许递归,但默认情况下是禁用的。要启用递归,请将 max_sp_recursion_depth 服务器系统变量设置为大于零的值。存储过程递归会增加线程栈空间的需求。如果增加 max_sp_recursion_depth 的值,可能需要通过在服务器启动时增加 thread_stack 的值来增加线程栈大小。更多信息,请参见 第 7.1.8 节,“服务器系统变量”。Recursion in stored procedures is permitted but disabled by default. To enable recursion, set the max_sp_recursion_depth server system variable to a value greater than zero. Stored procedure recursion increases the demand on thread stack space. If you increase the value of max_sp_recursion_depth, it may be necessary to increase thread stack size by increasing the value of thread_stack at server startup. See Section 7.1.8, “Server System Variables”, for more information.

MySQL 支持一个非常有用的扩展,使得可以在存储过程中使用常规的 SELECT 语句(即,不使用游标或本地变量)。此类查询的结果集会直接发送给客户端。多个 SELECT 语句会生成多个结果集,因此客户端必须使用支持多个结果集的 MySQL 客户端库。这意味着客户端必须使用至少为 4.1 版本的 MySQL 客户端库。客户端在连接时还应指定 CLIENT_MULTI_RESULTS 选项。对于 C 程序,这可以通过 mysql_real_connect() C API 函数来完成。请参阅 mysql_real_connect(),以及 多语句执行支持。MySQL supports a very useful extension that enables the use of regular SELECT statements (that is, without using cursors or local variables) inside a stored procedure. The result set of such a query is simply sent directly to the client. Multiple SELECT statements generate multiple result sets, so the client must use a MySQL client library that supports multiple result sets. This means the client must use a client library from a version of MySQL at least as recent as 4.1. The client should also specify the CLIENT_MULTI_RESULTS option when it connects. For C programs, this can be done with the mysql_real_connect() C API function. See mysql_real_connect(), and Multiple Statement Execution Support.

存储过程中的语句引用的用户变量,其类型在第一次调用该过程时确定,并在此后的每次调用中保持该类型。A user variable referenced by a statement in a stored procedure has its type determined the first time the procedure is invoked, and retains this type each time the procedure is invoked thereafter.