8.4 中文镜像

MySQL 8.4 参考手册 · 中文镜像

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

打开官方英文原文 ↗

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

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

15.2.1 CALL 语句15.2.1 CALL Statement

CALL sp_name([parameter[,...]])
CALL sp_name[()]

CALL 语句调用之前使用 CREATE PROCEDURE 定义的存储过程。The CALL statement invokes a stored procedure that was defined previously with CREATE PROCEDURE.

不带参数的存储过程可以在没有括号的情况下调用。也就是说,CALL p() 和 CALL p 是等价的。Stored procedures that take no arguments can be invoked without parentheses. That is, CALL p() and CALL p are equivalent.

CALL 可以通过声明为 OUT 或 INOUT 的参数向调用者返回值。当过程返回时,客户端程序还可以获得例程中最终执行的语句所影响的行数:在 SQL 层面,调用 ROW_COUNT() 函数;在 C API 中,调用 mysql_affected_rows() 函数。CALL can pass back values to its caller using parameters that are declared as OUT or INOUT parameters. When the procedure returns, a client program can also obtain the number of rows affected for the final statement executed within the routine: At the SQL level, call the ROW_COUNT() function; from the C API, call the mysql_affected_rows() function.

有关未处理条件对过程参数影响的信息,请参见 第 15.6.7.8 节,“条件处理及 OUT 或 INOUT 参数”。For information about the effect of unhandled conditions on procedure parameters, see Section 15.6.7.8, “Condition Handling and OUT or INOUT Parameters”.

要通过 OUT 或 INOUT 参数从过程获取值,请使用用户变量传递参数,然后在过程返回后检查变量的值。(如果你在另一个存储过程或函数中调用该过程,还可以将例程参数或本地例程变量作为 IN 或 INOUT 参数传递。)对于 INOUT 参数,在传递给过程之前应初始化其值。下列过程具有一个 OUT 参数,该过程将其设置为当前服务器版本,以及一个 INOUT 值,该过程会在当前值的基础上加一:To get back a value from a procedure using an OUT or INOUT parameter, pass the parameter by means of a user variable, and then check the value of the variable after the procedure returns. (If you are calling the procedure from within another stored procedure or function, you can also pass a routine parameter or local routine variable as an IN or INOUT parameter.) For an INOUT parameter, initialize its value before passing it to the procedure. The following procedure has an OUT parameter that the procedure sets to the current server version, and an INOUT value that the procedure increments by one from its current value:

DELIMITER //

CREATE PROCEDURE p (OUT ver_param VARCHAR(25), INOUT incr_param INT)
BEGIN
  # Set value of OUT parameter
  SELECT VERSION() INTO ver_param;
  # Increment value of INOUT parameter
  SET incr_param = incr_param + 1;
END //

DELIMITER ;

在调用过程之前,初始化将作为 INOUT 参数传递的变量。调用过程之后,你可以看到两个变量的值已被设置或修改:Before calling the procedure, initialize the variable to be passed as the INOUT parameter. After calling the procedure, you can see that the values of the two variables are set or modified:

mysql> SET @increment = 10;
mysql> CALL p(@version, @increment);
mysql> SELECT @version, @increment;
+----------+------------+
| @version | @increment |
+----------+------------+
| 8.4.9   |         11 |
+----------+------------+

在与 PREPARE 和 EXECUTE 一起使用的预处理 CALL 语句中,可以为 IN 参数、OUT 和 INOUT 参数使用占位符。这些类型的参数可以按如下方式使用:In prepared CALL statements used with PREPARE and EXECUTE, placeholders can be used for IN parameters, OUT, and INOUT parameters. These types of parameters can be used as follows:

mysql> SET @increment = 10;
mysql> PREPARE s FROM 'CALL p(?, ?)';
mysql> EXECUTE s USING @version, @increment;
mysql> SELECT @version, @increment;
+----------+------------+
| @version | @increment |
+----------+------------+
| 8.4.9   |         11 |
+----------+------------+

要编写使用 CALL SQL 语句来执行产生结果集的存储过程的 C 语言程序,必须启用 CLIENT_MULTI_RESULTS 标志。这是因为每个 CALL 返回调用状态的结果,除了过程内执行的语句可能返回的结果集外。CLIENT_MULTI_RESULTS也必须启用,如果 CALL用于执行包含预备语句的存储过程。无法确定加载此类过程时这些语句是否产生结果集,因此必须假设它们会产生结果集。To write C programs that use the CALL SQL statement to execute stored procedures that produce result sets, the CLIENT_MULTI_RESULTS flag must be enabled. This is because each CALL returns a result to indicate the call status, in addition to any result sets that might be returned by statements executed within the procedure. CLIENT_MULTI_RESULTS must also be enabled if CALL is used to execute any stored procedure that contains prepared statements. It cannot be determined when such a procedure is loaded whether those statements produce result sets, so it is necessary to assume that they do so.

CLIENT_MULTI_RESULTS 可以启用,当你调用 mysql_real_connect()时,可以通过显式传递 CLIENT_MULTI_RESULTS 标志来实现,要么通过隐式传递 CLIENT_MULTI_STATEMENTS(这也启用了 CLIENT_MULTI_RESULTS)。CLIENT_MULTI_RESULTS默认启用。CLIENT_MULTI_RESULTS can be enabled when you call mysql_real_connect(), either explicitly by passing the CLIENT_MULTI_RESULTS flag itself, or implicitly by passing CLIENT_MULTI_STATEMENTS (which also enables CLIENT_MULTI_RESULTS). CLIENT_MULTI_RESULTS is enabled by default.

处理 CALL 语句 mysql_query() 或 mysql_real_query()),使用一个循环调用 mysql_next_result() 来判断是否还有更多结果。例如,参见 多语句执行支持。To process the result of a CALL statement executed using mysql_query() or mysql_real_query(), use a loop that calls mysql_next_result() to determine whether there are more results. For an example, see Multiple Statement Execution Support.

C 程序可以使用准备语句接口执行 CALL 语句,访问 OUT 和 INOUT 参数。这是通过处理 CALL 语句,使用一个循环调用 mysql_stmt_next_result() 来判断是否有更多结果。示例可见 Prepared CALL Statement Support。提供 MySQL 接口的语言可以使用 prepared CALL 语句直接检索 OUT 和 INOUT 过程参数。C programs can use the prepared-statement interface to execute CALL statements and access OUT and INOUT parameters. This is done by processing the result of a CALL statement using a loop that calls mysql_stmt_next_result() to determine whether there are more results. For an example, see Prepared CALL Statement Support. Languages that provide a MySQL interface can use prepared CALL statements to directly retrieve OUT and INOUT procedure parameters.

存储程序引用对象的元数据变更会被检测到,并在程序下一次执行时自动解析受影响语句。更多信息请参见 第 10.10.3 节,“缓存已准备好的语句和存储程序”。Metadata changes to objects referred to by stored programs are detected and cause automatic reparsing of the affected statements when the program is next executed. For more information, see Section 10.10.3, “Caching of Prepared Statements and Stored Programs”.