MySQL Tutorial

MySQL Tutorial MySQL Features MySQL Database Introduction MySQL Environmental Setup MySQL Data Types MySQL variable MySQL Advance table Query MySQL database queries MySQL Entity-Relationship Model MySQL Table Query MySQL Operators MySQL logical conditions MySQL Queries MySQL Clauses Clustered vs Non-Clustered Index MySQL Full text index MySQL Descending Index MySQL Invisible Index MySQL Composite Index MySQL Prefix index MySQL Index MySQL Create index MySQL Drop Index MySQL Show index MySQL Unique index MySQL Table MySQL Variable MySQL View MySQL Constraints MySQL Command Line Client Basic Queries MySQL Stored Procedure MySQL IF Statement MySQL Subquery MySQL Triggers

MySQL Join

MySQL Join MySQL CROSS JOIN MySQL DELETE JOIN MySQL EQUI JOIN MySQL INNER JOIN MySQL Union MySQL NATURAL JOIN MySQL RIGHT JOIN MySQL SELF JOIN MySQL UPDATE JOIN

MySQL Function

MySQL Function MySQL AVG() Function MySQL SUM() Function MySQL String() Function MySQL Advance() Function MySQL Aggregate() Function MySQL COALESCE() Function MySQL Control Flow Function MySQL COUNT() Function MySQL Date And Time Function MySQL GREATEST() Function MySQL ISNULL() Function MySQL LEAST() Function MySQL Math() Function MySQL MAX() Function MySQL MIN() Function MySQL find_in_set() function MySQL ASIN() Function MySQL CEIL() function MySQL CEILING() function MySQL TAN() Function MySQL Truncate() Function MySQL FLOOR() function MySQL LN() function MySQL LOG2() function MySQL LOG10() function MySQL MOD() function MySQL PI() function MySQL POW() function MySQL RADIANS() function MySQL RAND() function MySQL ROUND() function MySQL Character Length Function MySQL Current Date Function MySQL Date Add Function MySQL Date Format Function MySQL Datediff Function MySQL Day Function MySQL Elt Function MySQL Export Set Function MySQL Field Function MySQL Format Function MySQL From Base64 Function MySQL Hex Function MySQL Insert Function MySQL Instr Function MySQL Length Function MySQL CONCAT() function MySQL FIND_IN_SET() function MySQL LIKE() function MySQL LOAD_FILE() function MySQL LOCATE() function MySQL LOG() function MySQL MONTHNAME() function MySQL NOW() function MySQL PERIOD_ADD() function MySQL PERIOD_DIFF() function MySQL POWER() function MySQL QUARTER() function MySQL REVERSE() function MySQL RIGHT() Function MySQL RPAD() function MySQL RTRIM() function MySQL SEC_TO_TIME() function MySQL SOUNDEX() function

Questions

Which Clause is Similar to Having Clause in MySQL

Misc

MySQL Error 1046 - No Database Selected Failed to Start MySQL Service Unit MySQL Service Unit not Found Import MySQL Connector Mudule not Found Error No Module Named MySQL Joins Available in MySQL MySQL Docs MySQL Download For Windows 7 64 Bit MySQL Error Code 1064 MySQL Export MySQL History MySQL Host MySQL Import MySQL Drop All Tables MySQL Drop MySQL Error Code 1175 MySQL Events MySQL Except MYSQL Foreign Key Constraint MySQL If Exists MySQL IndexOf MySQL List All Tables json_extract in MySQL TIMESTAMPDIFF in MySQL MySQL Syntax Checker Sudo MySQL Secure Installation

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.