13.2.1 CALL Statement语句

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

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

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

CALL can pass back values to its caller using parameters that are declared as OUT or INOUT parameters. CALL可以使用声明为OUTINOUT参数的参数将值传递回调用方。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.当过程返回时,客户机程序还可以获取例程中执行的最终语句所受影响的行数:在SQL级别,调用ROW_COUNT()函数;在C API中,调用mysql_infected_rows()函数。

For information about the effect of unhandled conditions on procedure parameters, see Section 13.6.7.8, “Condition Handling and OUT or INOUT Parameters”.有关未处理条件对程序参数影响的信息,请参阅第13.6.7.8节,“条件处理和输出或输入参数”

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. 要使用OUTINOUT参数从过程中获取值,请通过用户变量传递参数,然后在过程返回后检查变量的值。(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.) (如果从另一个存储过程或函数中调用该过程,也可以将例程参数或局部例程变量作为ININOUT参数传递。)For an INOUT parameter, initialize its value before passing it to the procedure. 对于INOUT参数,在将其传递给过程之前初始化其值。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:以下过程有一个OUT参数,该参数被设置为当前服务器版本,还有一个INOUT值,该值由该过程的当前值递增一:

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;

Before calling the procedure, initialize the variable to be passed as the INOUT parameter. 在调用过程之前,初始化要作为INOUT参数传递的变量。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.0.3-rc-debug-log |         11 |
+--------------------+------------+

In prepared CALL statements used with PREPARE and EXECUTE, placeholders can be used for IN parameters, OUT, and INOUT parameters. 在与PREPAREEXECUTE一起使用的准备好的CALL语句中,占位符可以用于IN参数、OUT参数和INOUT参数。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.0.3-rc-debug-log |         11 |
+--------------------+------------+

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. 要编写使用CALL SQL语句执行生成结果集的存储过程的C程序,必须启用CLIENT_MULTI_RESULTS标志。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. 这是因为除了过程中执行的语句可能返回的任何结果集之外,每个CALL还返回一个结果来指示调用状态。CLIENT_MULTI_RESULTS must also be enabled if CALL is used to execute any stored procedure that contains prepared statements. 如果CALL用于执行包含准备语句的任何存储过程,则还必须启用CLIENT_MULTI_RESULTSIt 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 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). 调用mysql_real_connect()时,可以显式地通过传递CLIENT_MULTI_RESULTS标志本身来启用CLIENT_MULTI_RESULTS,或者隐式地通过传递CLIENT_MULTI_STATEMENTS(这也可以启用CLIENT_MULTI_RESULTS)。CLIENT_MULTI_RESULTS is enabled by default.CLIENT_MULTI_RESULTS是默认启用的。

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. 要处理使用mysql_query()mysql_real_query()执行的CALL语句的结果,请使用调用mysql_next_result()的循环来确定是否有更多结果。For an example, see Multiple Statement Execution Support.有关示例,请参见多语句执行支持

C programs can use the prepared-statement interface to execute CALL statements and access OUT and INOUT parameters. C程序可以使用准备好的语句接口执行CALL语句并访问OUTINOUT参数。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. 这是通过使用调用mysql_stmt_next_result()的循环来处理CALL语句的结果来完成的,以确定是否有更多的结果。For an example, see Prepared CALL Statement Support. 有关示例,请参见准备好的CALL语句支持Languages that provide a MySQL interface can use prepared CALL statements to directly retrieve OUT and INOUT procedure parameters.提供MySQL接口的语言可以使用准备好的CALL语句直接检索OUTINOUT过程参数。

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 8.10.3, “Caching of Prepared Statements and Stored Programs”.有关更多信息,请参阅第8.10.3节,“准备语句和存储程序的缓存”