CALLsp_name
([parameter
[,...]]) CALLsp_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
可以使用声明为OUT
或INOUT
参数的参数将值传递回调用方。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 当过程返回时,客户机程序还可以获取例程中执行的最终语句所受影响的行数:在SQL级别,调用ROW_COUNT()
function; from the C API, call the mysql_affected_rows()
function.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. OUT
或INOUT
参数从过程中获取值,请通过用户变量传递参数,然后在过程返回后检查变量的值。(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.) IN
或INOUT
参数传递。)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. PREPARE
和EXECUTE
一起使用的准备好的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_RESULTS
。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
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 C程序可以使用准备好的语句接口执行CALL
statements and access OUT
and INOUT
parameters. CALL
语句并访问OUT
和INOUT
参数。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 提供MySQL接口的语言可以使用准备好的CALL
statements to directly retrieve OUT
and INOUT
procedure parameters.CALL
语句直接检索OUT
和INOUT
过程参数。
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节,“准备语句和存储程序的缓存”。