MySQL Reference Manual for version 4.0.18.

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

18.1 Stored Procedure Syntax

Stored procedures and functions are routines that are created with CREATE PROCEDURE and CREATE FUNCTION statements. A procedure is invoked using a CALL statement, and can only pass back values using output variables. Functions may return a scalar value and can be called from inside a statement just like any other function (that is, by invoking the function's name). Stored routines may call other stored routines. A routine is either a procedure or a function.

At present, MySQL only preserves context for the default database. That is, if you say USE dbname within a procedure, the original default database is restored upon routine exit. A routine inherits the default database from the caller, so generally routines should either issue a USE dbname statement, or specify all tables with an explicit database reference, e.g. dbname.tablename.

MySQL supports the very useful extension that allows 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.

This following section describes the syntax used to create, alter, drop, and query stored procedures and functions.

18.1.1 Maintaining Stored Procedures  
18.1.2 SHOW PROCEDURE STATUS and SHOW FUNCTION STATUS  
18.1.3 CALL  
18.1.4 BEGIN ... END Compound Statement  
18.1.5 DECLARE Statement  
18.1.6 Variables in Stored Procedures  
18.1.7 Conditions and Handlers  
18.1.8 Cursors  
18.1.9 Flow Control Constructs  


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

This document was generated by rdg (Feb 25 2004) using texi2html