JavaScript Editor Source code editor     What Is Ajax 

Main Page

Chapter 17. Stored Procedures and Functions

Table of Contents

17.1. Stored Routines and the Grant Tables
17.2. Stored Routine Syntax
17.2.1. CREATE PROCEDURE and CREATE FUNCTION Syntax
17.2.2. ALTER PROCEDURE and ALTER FUNCTION Syntax
17.2.3. DROP PROCEDURE and DROP FUNCTION Syntax
17.2.4. CALL Statement Syntax
17.2.5. BEGIN ... END Compound Statement Syntax
17.2.6. DECLARE Statement Syntax
17.2.7. Variables in Stored Routines
17.2.8. Conditions and Handlers
17.2.9. Cursors
17.2.10. Flow Control Constructs
17.3. Stored Procedures, Functions, Triggers, and LAST_INSERT_ID()
17.4. Binary Logging of Stored Routines and Triggers

Stored routines (procedures and functions) are supported in MySQL 5.0. A stored procedure is a set of SQL statements that can be stored in the server. Once this has been done, clients don't need to keep reissuing the individual statements but can refer to the stored procedure instead.

Answers to some questions that are commonly asked regarding stored routines in MySQL can be found in Section A.4, “MySQL 5.0 FAQ — Stored Procedures”.

MySQL Enterprise.  For expert advice on using stored procedures and functions subscribe to the MySQL Network Monitoring and Advisory Service. For more information see http://www.mysql.com/products/enterprise/advisors.html.

Some situations where stored routines can be particularly useful:

Stored routines can provide improved performance because less information needs to be sent between the server and the client. The tradeoff is that this does increase the load on the database server because more of the work is done on the server side and less is done on the client (application) side. Consider this if many client machines (such as Web servers) are serviced by only one or a few database servers.

Stored routines also allow you to have libraries of functions in the database server. This is a feature shared by modern application languages that allow such design internally (for example, by using classes). Using these client application language features is beneficial for the programmer even outside the scope of database use.

MySQL follows the SQL:2003 syntax for stored routines, which is also used by IBM's DB2.

The MySQL implementation of stored routines is still in progress. All syntax described in this chapter is supported and any limitations and extensions are documented where appropriate. Further discussion of restrictions on use of stored routines is given in Section F.1, “Restrictions on Stored Routines and Triggers”.

Binary logging for stored routines takes place as described in Section 17.4, “Binary Logging of Stored Routines and Triggers”.

Recursive stored procedures are disabled by default, but can be enabled on the server by setting the max_sp_recursion_depth server system variable to a nonzero value. See Section 5.2.3, “System Variables”, for more information.

Stored functions cannot be recursive. See Section F.1, “Restrictions on Stored Routines and Triggers”.

17.1. Stored Routines and the Grant Tables

Stored routines require the proc table in the mysql database. This table is created during the MySQL 5.0 installation procedure. If you are upgrading to MySQL 5.0 from an earlier version, be sure to update your grant tables to make sure that the proc table exists. See Section 5.5.8, “mysql_upgrade — Check Tables for MySQL Upgrade”.

The server manipulates the mysql.proc table in response to statements that create, alter, or drop stored routines. It is not supported that the server will notice manual manipulation of this table.

Beginning with MySQL 5.0.3, the grant system takes stored routines into account as follows:

  • The CREATE ROUTINE privilege is needed to create stored routines.

  • The ALTER ROUTINE privilege is needed to alter or drop stored routines. This privilege is granted automatically to the creator of a routine if necessary, and dropped when the routine creator drops the routine.

  • The EXECUTE privilege is required to execute stored routines. However, this privilege is granted automatically to the creator of a routine if necessary (and dropped when the creator drops the routine). Also, the default SQL SECURITY characteristic for a routine is DEFINER, which enables users who have access to the database with which the routine is associated to execute the routine.

  • If the automatic_sp_privileges system variable is 0, the EXECUTE and ALTER ROUTINE privileges are not automatically granted and dropped.

JavaScript Editor Source code editor     What Is Ajax