×

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 LEAST function

This statement displays the smallest value of the table. The LEAST function returns a null value when the table contains a null value. If the table contains all numerical values,...

2 minutes read.

MySQL POW() function

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

2 minutes read.

MySQL SOUNDEX() function

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

3 minutes read.

MySQL SUM function

The SUM() function displays the total addition of the table values. It supports the arithmetic operation of the column values. This function does not return a null value. Here, the...

5 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 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 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 Show index

This query helps to access and retrieve table information. This syntax displays the required index or all indexes of the table. It also displays the index type as per the...

3 minutes read.

MySQL LN() function

In this context, we will learn how we can use the MySQL LN() function with proper syntax and good examples. Introduction of MySQL LN() function For evaluation of the natural logarithm of...

2 minutes read.

MySQL Aggregate function

The aggregation function works on the multiple values and returns single value output.  It makes the advanced and complex operation simple. The summation (SUM), MAX, AVG, MIN, COUNT functions are...

5 minutes read.

MySQL GREATEST function

This statement displays the greatest values of the table. The GREATEST function returns a null value when the table contains a null value. The GREATEST function needs a minimum of...

2 minutes read.

MySQL Vs MongoDB

What is MongoDB? MongoDB is a distributed, open-source, cross-platform document-based database which was created to scale and develop applications simply. It was created as a NoSQL database by MongoDB Inc. MongoDB gets...

6 minutes read.

MySQL ISNULL function

This function is used to check the null value in table data. It returns the result in a Boolean form. If table data is null, then the output becomes 1....

2 minutes read.

MySQL RTRIM() function

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

2 minutes read.

MySQL MOD() 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 Basically, in MySQL, the MOD() function is...

3 minutes read.

MySQL RAND() function

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

3 minutes read.

MySQL Clauses

MySQL Clauses MySQL system clauses are keywords or statements to handle information. It helps to operate a group of the data and apply it to require conditions. The clauses apply conditions...

16 minutes read.

MySQL ELT() function

In this context, we will learn how we can use the MySQL ELT() function with proper syntax and good examples. Introduction of MySQL ELT() function In the ELT function, the number field...

2 minutes read.

MySQL Date and Time function

The date function displays day, year, month, time, and current date. It shows the date and time as per the requirement of the applications. The data either store on the...

8 minutes read.

MySQL FIELD() function

In this context, we will learn how we can use the MySQL FIELD() function with proper syntax and good examples. Introduction of MySQL FIELD() function The index position of a mentioned value...

3 minutes read.