Does MySQL 5.7 support stored procedures and functions?
Yes. MySQL 5.7 supports two types of stored routines—stored procedures and stored functions.
Where can I find documentation for MySQL stored procedures and stored functions?
Is there a discussion forum for MySQL stored procedures?
Yes. See list.php?98.
Where can I find the ANSI SQL 2003 specification for stored procedures?
Unfortunately, the official specifications are not freely available (ANSI makes them available for purchase). However, there are books—such as SQL-99 Complete, Really by Peter Gulutzan and Trudy Pelzer—which give a comprehensive overview of the standard, including coverage of stored procedures.
How do you manage stored routines?
It is always good practice to use a clear naming scheme for your
stored routines. You can manage stored procedures with
Is there a way to view all stored procedures and stored functions in a given database?
Yes. For a database named
SELECT ROUTINE_TYPE, ROUTINE_NAME FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_SCHEMA='
For more information, see Section 20.19, “The INFORMATION_SCHEMA ROUTINES Table”.
The body of a stored routine can be viewed using
Where are stored procedures stored?
You can also query the
Is it possible to group stored procedures or stored functions into packages?
No. This is not supported in MySQL 5.7.
Can a stored procedure call another stored procedure?
Can a stored procedure call a trigger?
A stored procedure can execute an SQL statement, such as an
Can a stored procedure access tables?
Yes. A stored procedure can access one or more tables as required.
Do stored procedures have a statement for raising application errors?
Yes. MySQL 5.7 implements the SQL standard
Do stored procedures provide exception handling?
Can MySQL 5.7 stored routines return result sets?
Stored procedures can, but stored functions
cannot. If you perform an ordinary
Not in MySQL 5.7.
Is there a MySQL equivalent to using
There is no equivalent in MySQL 5.7.
Can I pass an array as input to a stored procedure?
Not in MySQL 5.7.
Can I pass a cursor as an
In MySQL 5.7, cursors are available inside stored procedures only.
Can I return a cursor as an
In MySQL 5.7, cursors are available inside stored
procedures only. However, if you do not open a cursor on a
Can I print out a variable's value within a stored routine for debugging purposes?
Yes, you can do this in a stored procedure,
but not in a stored function. If you perform an ordinary
Can I commit or roll back transactions inside a stored procedure?
Yes. However, you cannot perform transactional operations within a stored function.
Do MySQL 5.7 stored procedures and functions work with replication?
Yes, standard actions carried out in stored procedures and functions are replicated from a master MySQL server to a slave server. There are a few limitations that are described in detail in Section 19.7, “Binary Logging of Stored Programs”.
Are stored procedures and functions created on a master server replicated to a slave?
Yes, creation of stored procedures and functions carried out
through normal DDL statements on a master server are replicated
to a slave, so the objects will exist on both servers.
How are actions that take place inside stored procedures and functions replicated?
MySQL records each DML event that occurs in a stored procedure and replicates those individual actions to a slave server. The actual calls made to execute stored procedures are not replicated.
Stored functions that change data are logged as function invocations, not as the DML events that occur inside each function.
Are there special security requirements for using stored procedures and functions together with replication?
Yes. Because a slave server has authority to execute any statement read from a master's binary log, special security constraints exist for using stored functions with replication. If replication or binary logging in general (for the purpose of point-in-time recovery) is active, then MySQL DBAs have two security options open to them:
What limitations exist for replicating stored procedure and function actions?
Nondeterministic (random) or time-based actions embedded in
stored procedures may not replicate properly. By their very
nature, randomly produced results are not predictable and cannot
be exactly reproduced, and therefore, random actions replicated
to a slave will not mirror those performed on a master. Note
that declaring stored functions to be
In addition, time-based actions cannot be reproduced on a slave because the timing of such actions in a stored procedure is not reproducible through the binary log used for replication. It records only DML events and does not factor in timing constraints.
Finally, nontransactional tables for which errors occur during
large DML actions (such as bulk inserts) may experience
replication issues in that a master may be partially updated
from DML activity, but no updates are done to the slave because
of the errors that occurred. A workaround is for a function's
DML actions to be carried out with the
Do the preceding limitations affect MySQL's ability to do point-in-time recovery?
The same limitations that affect replication do affect point-in-time recovery.
What is being done to correct the aforementioned limitations?
You can choose either statement-based replication or row-based replication. The original replication implementation is based on statement-based binary logging. Row-based binary logging resolves the limitations mentioned earlier.
Mixed replication is also available (by
starting the server with
For additional information, see Section 17.2.1, “Replication Formats”.