存储例程可以是过程或函数。存储例程通过 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 implicitUSEis performed (and undone when the routine terminates).db_nameUSEstatements 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 procedurepor functionfthat is associated with thetestdatabase, you can sayCALL test.p()ortest.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.