×

MySQL Stored Procedure

MySQL creates the "stored procedure" function to operate database information. You can use parameters, blocks, and statements to create a new procedure. The procedure requires a database table to use as a reference. You can use the command-line client interface or workbench interface for MySQL stored procedure.

Syntax of creating MySQL stored procedure

Syntax with parameter

The following syntax is used to create stored procedures in the database.

DELIMITER //
CREATE PROCEDURE storeprocedure_name(parameter mode / parameter list)
BEGIN 
MySQL statement;
END//
DELIMTER;

This query is used to create a procedure to retrieve database information. The delimiter is necessary for the stored procedure. The double slash represents the starting point of the procedure. The semicolon after delimiter represents the endpoint of the stored procedure.

Syntax without parameter

The following syntax is used to create a stored procedure without parameters in the database.

DELIMITER //
CREATE PROCEDURE storeprocedure_name( )
BEGIN 
MySQL statement;
END//
DELIMTER;

The stored procedure does not contain parameters every time. You can create a procedure without a parameter list and mode.

Prerequisite of MySQL stored procedure

  • Create or use a database in the MySQL system interface for table operation.
mysql > Use database_names;
  • If MySQL database does not exist in the system, then create a new database.
mysql > create database database_names;
  • Create a table with columns, data types, and basic constraints of MySQL.
CREATE TABLE emp_informations (
    emp_id int,
    emp_name varchar(155),
    emp_department varchar(155),
    emp_position varchar(65),
    emp_salary int,
    emp_phone bigint,
  PRIMARY KEY(emp_id)
);
  • Insert information in the table according to MySQL validations.
INSERT INTO emp_informations (
    emp_id , emp_name ,  emp_department , emp_position,
    emp_salary, emp_phone
) values
(1, "aasha", "elecetrical", "employee", 12000, 7878463212),
(2, "Mahesh", "elecetrical", "senior", 50000, 7775463213),
(3, "radha", "elecetrical", "employee", 15000, 7975463216),
(4, "sadhana", "elecetrical", "maintenance", 25000, 9875483212),
(5, "samir", "elecetrical", "HR", 35000, 8875463212),
(6, "rose", "elecetrical", "trainee", 10000, 7275463212);
  • Show the given table using MySQL query.
Select * from emp_informations;
Create MySQL Stored Procedure

EXAMPLE of the MySQL stored procedure

Example 1: It is the basic MySQL stored procedure example and output.

The following example is used to create a stored procedure. This procedure comes without parameter mode and parameter list.

mysql> DELIMITER //
mysql> CREATE PROCEDURE info_employee( )
BEGIN 
Select * from emp_informations;
END//
Create MySQL Stored Procedure

This image shows how to create MySQL stored procedures in the command-line client interface. The row does not affect the procedure, and the statement displays the required data. You can call the stored procedure to get output.

Output

The following query is used to get the output of the stored procedure.

mysql> CALL info_employee( );
          -> //
Create MySQL Stored Procedure

The call function displays all required information of the table in the stored procedure. The statement shows table data with its columns and rows.

Example 2: The MySQL stored procedure with declaration and executable statements example and output.

Execute the below query to create MySQL stored procedure. This stored procedure works without a parameter list.

mysql> DELIMITER //
mysql> CREATE PROCEDURE info_employees( )
BEGIN 
Select * from emp_informations LIMIT 3;
Select max(emp_salary) AS Max_salary from emp_informations;
END//
Create MySQL Stored Procedure

Here, you can see two statements inside of the block (BEGIN and END). The call function displays both statements output in one query.

Output

mysql> call info_employees;
         -> //
Create MySQL Stored Procedure

The image shows two output tables as per the statement. The first table shows the first statement of the procedure. This table displays information with three rows limit. The second table represents the second max statement of the procedure. This query shows the highest salary row and column of the table.

Example 3: The MySQL stored procedure with declaration and executable statements example and output.

The below example is used to create MySQL stored procedure. This stored procedure works with the "IN" parameter list.

mysql> DELIMITER //
mysql> CREATE PROCEDURE info_employes( IN salary BIGINT )
BEGIN 
Select * from emp_informations WHERE emp_salary > salary;
END//
Create MySQL Stored Procedure

You can see a single statement with IN parameter in the block (BEGIN and END). The function works with the parameter mode and conditional operator for the salary column.

Output

mysql> call info_employes (25000);
         -> //
Create MySQL Stored Procedure

The above output image shows two rows of the table. This table's data shows information using salary greater than the 25000.


Related Topics

MySQL CROSS JOIN

MySQL CROSS JOIN combines all possibilities of the two or more tables and returns the result that contains every row from all contributing tables. It links several columns from the...

4 minutes read.

MySQL NOW() function

In this context, we will learn how we can use the MySQL NOW() function with proper syntax and good examples. Introduction of MySQL NOW() function Basically, in MySQL, this function is used...

2 minutes read.

MySQL Unique index

It helps to maintain data integrity to enforce the uniqueness of values in one or more columns. We can create more than one UNIQUE index in a single table, which...

3 minutes read.

MySQL Join

The relational database system needs to interconnect multiple tables with each other. MySQL is a popular and easy data management system to connect multiple tables. The foreign key is used...

5 minutes read.

MySQL Full text index

Full text index The full-text index in the table isused to search the full text of the data. This index assigns to the table using a "FULLTEXT" keyword. First, the table...

3 minutes read.

MySQL function

MySQL function is a piece of program to perform some specific task. Here we pass a parameter and then return a single value. MySQL function mainly supports string, numeric, conditional,...

9 minutes read.

MySQL INNER JOIN

MySQL inner join connects two tables by using their common columns. It is the basic join of the MySQL system. It is used to returns only those results from the...

4 minutes read.

MySQL Constraints

MySQL Constraints Introduction The constraints help to restrict what values should be stored in a table. The constraints provide limitations of the columns or data. This function helps to insert data in...

20 minutes read.

MySQL SELF JOIN

This join links table with itself. The inner join, a self join, a right join, and a cross join are connected with two or more two tables. But, the "self...

4 minutes read.

MySQL RIGHT() Function

In this context, we will learn how we can use the MySQL RIGHT() Function with proper syntax and good examples. Introduction of MySQL RIGHT() function The RIGHT() function in MySQL is used...

3 minutes read.

MySQL Invisible Index

Invisible Index MySQL index decides an option to the availability of the index for the query optimizer. This index shows the visible and invisible options to displays the index column in...

3 minutes read.

MySQL Drop Index

Sometimes we have an index that is not required in data operations. In that case, we can use this statement to remove an existing index from the table. We can...

4 minutes read.

MySQL ASIN() Function

In this context we will learn how we can use the ASIN() function in MySQL with proper syntax and good example. Introduction of MySQL ASIN() function The arc sine value of a...

2 minutes read.

MySQL Composite Index

Composite Index A composite index is an index that is used on multiple columns. MySQL management system manages multiple columns simultaneously. Therefore, the single index contains multiple columns in one query...

3 minutes read.

MySQL LOG10() function

In this context, we will learn how we can use the MySQL LOG10() function with proper syntax and good examples. Introduction of MySQL LOG10() function To evaluate the natural logarithmic value of...

2 minutes read.

MySQL FLOOR() function

In this context, we will learn how we can use the MySQL FLOOR() function with proper syntaxes and examples. Introduction of MySQL Floor() function: FLOOR() function in MySQL is used to return...

3 minutes read.

MySQL Table

Introduction MySQL table is an essential part of the system. MySQL table stores data using index, rows, and columns. It accesses data from the server quickly because of the table—this table...

22 minutes read.

MySQL LOCATE() function

In this context, we will learn how we can use the MySQL LOCATE() function with proper syntax and good examples. Introduction of MySQL LOCATE() function LOCATE() function in MySQL is used for...

3 minutes read.

MySQL vs Oracle

What is MySQL? The open-source MySQL relational database management system is a vital software component for web-based applications. As the data is saved and sent over the internet, databases and related...

6 minutes read.

MySQL FIND_IN_SET() function

In this context, we will learn how we can use the MySQL MOD() function with proper syntax and good examples. Introduction of MySQL MOD() function MySQL provides a built-in string function called...

4 minutes read.