×

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 CURRENT_DATE() Function

In this context, we will learn how we can use the MySQL CURRENT_DATE() function to get the current date, and we will see in the two formats, such as strings...

2 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 TAN() Function

Basically, the TAN() function in MySQL is used to give back the tangent of a mentioned number.In any kind of right-angle triangle, the tangent of an angle is the length...

2 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 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 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 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 REVERSE() function

In this context, we will learn how we can use the MySQL REVERSE() function with proper syntax and good examples. Introduction of MySQL REVERSE() function This function could be used to reverse...

2 minutes read.

MySQL DELETE JOIN

Sometimes join becomes complicated and unwanted. In such cases, we can delete the unwanted joins in the tables. You can delete inner join, left join, right join as per requirement....

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 HEX() function

In this context, we will learn how we can use the MySQL HEX() function with proper syntax and good examples. Introduction of MySQL HEX() function For returning an equivalent hexadecimal string value...

3 minutes read.

MySQL CEILING() function

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

2 minutes read.

MySQL COUNT function

The count function returns the number of rows in the table. This function shows either the entire rows count or the required row count of the table. It determines the...

5 minutes read.

MySQL Features

Features of MySQL MySQL is a relational database management system (RDBMS): It is a collection of many programs and makes relations with many other programs. Easy to use MySQL database: MySQL...

2 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: Entity-Relationship Model

Entity-Relationship Model Entity-Relationship model or E R model is used to create a relationship between different attributes or entities. It describes the structure of the database with the help of the...

5 minutes read.

MySQL LOG2() function

In this context, we will learn how we can use the MySQL LOG2() function to calculate the Logarithm of a specific number with base 2 with proper syntax and examples. Introduction...

2 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 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 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.