Version sans cache


Mise en cache désactivé. Réglage défaut pour cette page : actif (code DEF204)
Si l'affichage est trop lent, vous pouvez désactiver le mode utilisateur pour visualiser la version en cache.

Rechercher dans le manuel MySQL

24.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:

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

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.

  1. mysql> delimiter //
  2.  
  3. mysql> CREATE PROCEDURE dorepeat(p1 INT)
  4.     -> BEGIN
  5.     ->   SET @x = 0;
  6.     ->   REPEAT SET @x = @x + 1; UNTIL @x > p1 END REPEAT;
  7.     -> END
  8.     -> //
  9. Query OK, 0 rows affected (0.00 sec)
  10.  
  11. mysql> delimiter ;
  12.  
  13. mysql> CALL dorepeat(1000);
  14. Query OK, 0 rows affected (0.00 sec)
  15.  
  16. mysql> SELECT @x;
  17. +------+
  18. | @x   |
  19. +------+
  20. | 1001 |
  21. +------+
  22. 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:

  1. mysql> CREATE FUNCTION hello (s CHAR(20))
  2.     -> RETURN CONCAT('Hello, ',s,'!');
  3. Query OK, 0 rows affected (0.00 sec)
  4.  
  5. mysql> SELECT hello('world');
  6. +----------------+
  7. | hello('world') |
  8. +----------------+
  9. | Hello, world!  |
  10. +----------------+
  11. 1 row in set (0.00 sec)

Rechercher dans le manuel MySQL

Traduction non disponible

Le manuel MySQL n'est pas encore traduit en français sur l'infobrol. Seule la version anglaise est disponible pour l'instant.

Document créé le 26/06/2006, dernière modification le 26/10/2018
Source du document imprimé : https://www.gaudry.be/mysql-rf-stored-programs-defining.html

L'infobrol est un site personnel dont le contenu n'engage que moi. Le texte est mis à disposition sous licence CreativeCommons(BY-NC-SA). Plus d'info sur les conditions d'utilisation et sur l'auteur.

Références

  1. Consulter le document html Langue du document :en Manuel MySQL : https://dev.mysql.com/

Ces références et liens indiquent des documents consultés lors de la rédaction de cette page, ou qui peuvent apporter un complément d'information, mais les auteurs de ces sources ne peuvent être tenus responsables du contenu de cette page.
L'auteur de ce site est seul responsable de la manière dont sont présentés ici les différents concepts, et des libertés qui sont prises avec les ouvrages de référence. N'oubliez pas que vous devez croiser les informations de sources multiples afin de diminuer les risques d'erreurs.

Table des matières Haut