Create a stored procedure with delimiter in MySQL

MySQLMySQLi Database

You can create a stored procedure using create procedure command. The syntax is as follows −

delimiter //
CREATE PROCEDURE yourStoreProcedureName()
BEGIN
   Declare variable here
   The query statement
END //

delimiter //

Apply the above syntax to create a stored procedure. The query is as follows −

mysql> use test;
Database changed
mysql> delimiter //
mysql> create procedure Sp_callTableStoredProcTable()
   −> begin
   −> select *from StoredProcTable;
   −> end //
Query OK, 0 rows affected (0.54 sec)

Now you need to change the delimiter with ; to call stored procedure −

mysql> delimiter ;

You can call stored procedure using CALL command. The syntax is as follows −

CALL yourStoredProcedureName();

The above stored procedure can be called using CALL command as shown in the below query −

mysql> call Sp_callTableStoredProcTable();

The following is the output −

+-----------+------+
| FirstName | Age  |
+-----------+------+
| John      | 23   |
| Bob       | 24   |
| David     | 20   |
+-----------+------+
3 rows in set (0.03 sec)

Query OK, 0 rows affected (0.06 sec)

Above, we have used the table ‘StoredProcTable’ and this table has three records. Displayed all records using stored procedure.

You can check how many records are in the table using stored procedure −

mysql> delimiter //
mysql> create procedure CountingRecords()
   −> begin
   −> select count(*) as AllRecords from StoredProcTable;
   −> end //
Query OK, 0 rows affected (0.19 sec)

mysql> delimiter ;

Call the stored procedure using CALL command. The query is as follows −

mysql> call CountingRecords();

The following output displays the count of records −

+------------+
| AllRecords |
+------------+
|          3 |
+------------+
1 row in set (0.31 sec)

Query OK, 0 rows affected (0.33 sec)
raja
Published on 16-Jan-2019 12:18:32
Advertisements