How to correctly use DELIMITER in a MySQL stored procedure?


The correct way is as follows −

DELIMITER //
CREATE PROCEDURE yourStoredProcedureName()
BEGIN
 IF  yourCondition then
     yourStatement1 ;
else
    yourStatement2 ;

END IF ;
END
//

DELIMITER ;

Let us now see an example and create a stored procedure −

mysql> DELIMITER //
mysql> CREATE PROCEDURE delimiter_demo()
   -> BEGIN
   -> IF 1 THEN
   -> SELECT "If condition will always true";
   -> else
   -> select "No" ;
   -> END IF ;
   -> END
   -> //
Query OK, 0 rows affected (0.17 sec)

mysql> DELIMITER ;

Now you can call the stored procedure using CALL command

mysql> call delimiter_demo();

This will produce the following output −

+-------------------------------+
| If condition will always true |
+-------------------------------+
| If condition will always true |
+-------------------------------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.02 sec)

Updated on: 17-Dec-2019

194 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements