Related Documentation Download this Manual
PDF (US Ltr) - 35.7Mb
PDF (A4) - 35.8Mb
PDF (RPM) - 34.8Mb
EPUB - 8.7Mb
HTML Download (TGZ) - 8.5Mb
HTML Download (Zip) - 8.5Mb
HTML Download (RPM) - 7.3Mb
Eclipse Doc Plugin (TGZ) - 9.3Mb
Eclipse Doc Plugin (Zip) - 11.5Mb
Man Pages (TGZ) - 203.6Kb
Man Pages (Zip) - 309.0Kb
Info (Gzip) - 3.4Mb
Info (Zip) - 3.4Mb
Excerpts from this Manual

MySQL 5.7 Reference Manual  /  Stored Programs and Views  /  Defining Stored Programs

21.1 Defining Stored Programs

Each stored program contains a body that consists of an SQL statement. This statement may be a compound statement made up of several statements separated by semicolon (;) characters. For example, the following stored procedure has a body made up of a BEGIN ... END block that contains a SET statement and a REPEAT loop that itself contains another SET statement:

CREATE PROCEDURE dorepeat(p1 INT)
BEGIN
  SET @x = 0;
  REPEAT SET @x = @x + 1; UNTIL @x > p1 END REPEAT;
END;

If you use the mysql client program to define a stored program containing semicolon characters, a problem arises. By default, mysql itself recognizes the semicolon as a statement delimiter, so you must redefine the delimiter temporarily to cause mysql to pass the entire stored program definition to the server.

To redefine the mysql delimiter, use the delimiter command. The following example shows how to do this for the dorepeat() procedure just shown. The delimiter is changed to // to enable the entire definition to be passed to the server as a single statement, and then restored to ; before invoking the procedure. This enables the ; delimiter used in the procedure body to be passed through to the server rather than being interpreted by mysql itself.

mysql> delimiter //

mysql> CREATE PROCEDURE dorepeat(p1 INT)
    -> BEGIN
    ->   SET @x = 0;
    ->   REPEAT SET @x = @x + 1; UNTIL @x > p1 END REPEAT;
    -> END
    -> //
Query OK, 0 rows affected (0.00 sec)

mysql> delimiter ;

mysql> CALL dorepeat(1000);
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT @x;
+------+
| @x   |
+------+
| 1001 |
+------+
1 row in set (0.00 sec)

You can redefine the delimiter to a string other than //, and the delimiter can consist of a single character or multiple characters. You should avoid the use of the backslash (\) character because that is the escape character for MySQL.

The following is an example of a function that takes a parameter, performs an operation using an SQL function, and returns the result. In this case, it is unnecessary to use delimiter because the function definition contains no internal ; statement delimiters:

mysql> CREATE FUNCTION hello (s CHAR(20))
mysql> RETURNS CHAR(50) DETERMINISTIC
    -> RETURN CONCAT('Hello, ',s,'!');
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT hello('world');
+----------------+
| hello('world') |
+----------------+
| Hello, world!  |
+----------------+
1 row in set (0.00 sec)

User Comments
  Posted by Md. Mahmud Ahsan on April 26, 2010
Nice example. I created an article regarding creating some sample data using mysql stored procedure. http://thinkdiff.net/mysql/using-mysql-stored-procedure-to-create-sample-data/
  Posted by Douglas Boffey on January 7, 2012
If trying to create a STORED PROCEDURE from a text file, it is better to use the --delimeter=... command line option.
  Posted by David Bagnara on May 12, 2016
The code below I wrote to learn about stored procedures. I used it to restructure a couple of schemas. Hope it helps.

CREATE SCHEMA IF NOT EXISTS Tools ;
DROP PROCEDURE IF EXISTS Tools.copyTable ;

DELIMITER //
CREATE PROCEDURE Tools.copyTable( tableName VARCHAR(100), src VARCHAR(100), dest VARCHAR(100) )
BEGIN
set @src = CONCAT_WS( '.', src, tableName ) ;
set @dest = CONCAT_WS( '.', dest, tableName ) ;
set @createSchema = CONCAT_WS( ' ', 'create schema if not exists', dest ) ;
set @drop = CONCAT_WS( ' ', 'drop table if exists',@dest ) ;
set @create = CONCAT_WS( ' ', 'create table', @dest, 'like', @src ) ;
set @copy = CONCAT_WS( ' ', 'insert into', @dest, 'select * from', @src ) ;

prepare stmt from @createSchema ;
execute stmt ;
deallocate prepare stmt ;

prepare stmt from @drop ;
execute stmt ;
deallocate prepare stmt ;

prepare stmt from @create ;
execute stmt ;
deallocate prepare stmt ;

prepare stmt from @copy ;
execute stmt ;
deallocate prepare stmt ;
END //
DELIMITER ;

# set up the table copies here.
CALL Tools.copyTable( 'mytable', 'mySchema', 'anotherSchema' ) ;
CALL Tools.copyTable( 'myoldtable', 'mydb', 'deprecatedSchema' ) ;
#etc etc
Sign Up Login You must be logged in to post a comment.