MySQL: Create a stored procedure

In this tutorial you will learn how you can create a stored procedure in your MySQL database.

Log in to your database

delimiter //
CREATE PROCEDURE  (OUT value1 INT)
BEGIN
   SELECT COUNT(*) INTO value1 FROM ;
END;
//

Example

mysql> delimiter //
mysql> CREATE PROCEDURE countCustomers(OUT value1 INT)
    -> BEGIN
    -> SELECT COUNT(*) INTO value1 FROM customer;
    -> END;
    -> //
Query OK, 0 rows affected (0.00 sec)

Now use it.

mysql> delimiter ;
mysql> CALL countCustomers(@var);
Query OK, 1 row affected (0.00 sec)

mysql> Select @var;
+------+
| @var |
+------+
|    3 |
+------+
1 row in set (0.00 sec)

Notice, you can create more complex procedures as this counting procedure, this should only be a very simple example.

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.