Rechercher dans le manuel MySQL
24.2.1 Stored Routine Syntax
A stored routine is either a procedure or a function. Stored
routines are created with the CREATE
PROCEDURE
and CREATE
FUNCTION
statements (see
Section 13.1.17, “CREATE PROCEDURE and CREATE FUNCTION Syntax”). A procedure is invoked using
a CALL
statement (see
Section 13.2.1, “CALL Syntax”), 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 13.6, “Compound-Statement Syntax”).
Stored routines can be dropped with the DROP
PROCEDURE
and DROP
FUNCTION
statements (see
Section 13.1.29, “DROP PROCEDURE and DROP FUNCTION Syntax”), and altered with the
ALTER PROCEDURE
and
ALTER FUNCTION
statements (see
Section 13.1.7, “ALTER PROCEDURE Syntax”).
A stored procedure or function is associated with a particular database. This has several implications:
When the routine is invoked, an implicit
USE
is performed (and undone when the routine terminates).db_name
USE
statements within stored routines are not permitted.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 functionf
that is associated with thetest
database, 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.
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 5.1.8, “Server System Variables”, for more
information.
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 Section 28.7.7.54, “mysql_real_connect()”, and
Section 28.7.23, “C API Multiple Statement Execution Support”.
Document created the 26/06/2006, last modified the 26/10/2018
Source of the printed document:https://www.gaudry.be/en/mysql-rf-stored-routines-syntax.html
The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.
References
These references and links indicate documents consulted during the writing of this page, or which may provide additional information, but the authors of these sources can not be held responsible for the content of this page.
The author This site is solely responsible for the way in which the various concepts, and the freedoms that are taken with the reference works, are presented here. Remember that you must cross multiple source information to reduce the risk of errors.