
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
Create a stored procedure with delimiter in MySQL
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)
- Related Articles
- How to correctly use DELIMITER in a MySQL stored procedure?
- How to correctly use delimiter in a MySQL stored procedure and insert values?
- Create variables in MySQL stored procedure with DECLARE keyword
- MySQL Stored Procedure to create a table?
- Use delimiter correctly in a MySQL stored procedure to avoid BEGIN/END statements errors
- How can I create MySQL stored procedure with IN parameter?
- How can I create MySQL stored procedure with OUT parameter?
- How can I create MySQL stored procedure with INOUT parameter?
- Working with WHERE IN() in a MySQL Stored Procedure
- MySQL stored procedure to execute SHOW CREATE TABLE?
- Create a MySQL stored procedure that generates five random numbers?
- How can a MySQL stored procedure call another MySQL stored procedure inside it?
- Create a procedure in MySQL with parameters?
- What is stored procedure and how can we create MySQL stored procedures?
- What is STORED PROCEDURE in a DB2? How will you create a new stored procedure?
