Updated May 25, 2023
Introduction to MySQL Stored Procedure
The stored procedure can be called the subroutine that needs to be called, accepts parameters, and consists of a sequence of executed database queries. These queries can be executed on a conditional basis and looped around using the FOR loop statement. Most of the time, the MySQL stored procedure is used when your application involves specific modules having too many database operations and alterations of the tables, and all this needs o to be performed efficiently within a short timespan.
The stored procedure can allow us to work on multiple databases and execute queries involving tables of one or more than one database. We can even call other stored procedures from specific stored procedures. You can call these stored procedures as many times as desired. Note that stored procedures don’t return any values. In this article, we will learn about the stored procedures in MySQL, how we can create them call them for execution, and drop the stored procedures.
How to Create Stored Procedure in MySQL?
The stored procedure is a subroutine, specifically a procedure that you can create in MYSQL using the CREATE PROCEDURE statement.
Let us learn the syntax of creating the stored procedure:
CREATE PROCEDURE name_of_SP [(nameOfParameter1 datatypeOfParameter1 [,nameOfParameteri datatypeOfParameteri])]
BEGIN
// Declaration part of stored procedure
// Execution part of stored procedure
END;
1. name_of_SP: The stored procedure’s name needs to be created in MySQL.
2. NameOfParameter: We can pass the optional parameters to the stored procedures that must be declared while creating it in the () brackets. A stored procedure can contain none, one, or more than one parameter.
These parameters can belong to either of the three types:
- IN: You assign values to these parameters when calling the stored procedure, and the stored procedure references and utilizes these values. They cannot be modified or overwritten within the stored procedure.
- OUT:You can assign values to these parameters and override them in the stored procedure, but you cannot reference them directly.
- IN OUT: These parameters are assigned the values while calling the stored procedure, and the value can be modified or overwritten inside the stored procedure and referenced and used by the stored procedure.
3. BEGIN and END: BEGIN keyword marks the beginning of the stored procedure, while END marks the completion of the stored procedure in MYSQL.
4. Declaration part of the stored procedure: We can declare the local variables if we want any in this part of the stored procedure.
5. Execution part of the stored procedure: We can write our program or code in this section of the stored procedure that can contain conditional, looping statements, initializing and assigning the value of variables, and preparing and executing the database queries. You can also include calls to other stored procedures within this section of the stored procedure.
Example to Implement MySQL Stored Procedure
Below are some examples of MySQL Stored Procedures:
1. Let us create one simple stored procedure. Still, before that, I will create a sample table named last_tran_date that will store the date of the last transaction on the particular object along with its item code and label number using the following query:
CREATE TABLE 'last_tran_date' (
'itemCode' int(11) NOT NULL COMMENT 'ItemCode From Item Master',
'LabelNo' int(11) NOT NULL DEFAULT '0' COMMENT 'Generated Label No',
'LastTranDate' date DEFAULT NULL,
KEY 'By_item' ('itemCode','LabelNo')
) ENGINE=MyISAM DEFAULT CHARSET=latin1
And add some of the values using INSERT statement like the following:
INSERT INTO 'last_tran_date' ('itemCode', 'LabelNo', 'LastTranDate') VALUES('87','8529816','2019-12-03');
2. Let us confirm all the inserted values using the SELECT query:
SELECT * FROM 'last_tran_date';
Output:
Hence 30 rows are present in the last_tran_date table.
Creating the Stored Procedure in MySQL
Now, let’s create a stored procedure that returns the label numbers for which the last transaction date is passed as a parameter to the GetLabelsOfLastTransDate procedure. Our stored procedure will be as follows:
DELIMITER $$
CREATE PROCEDURE GetLabelsOfLastTransDate(IN last_trans_date DATE)
BEGIN
SELECT
LabelNo
FROM
`last_tran_date
WHERE LastTranDate = last_trans_date ;
END$$
DELIMITER ;
Calling the Stored Procedure in MySQL
Syntax:
Below is the syntax for calling or executing the stored procedure:
CALL name_of_SP (value of parameters accepted by stored procedure);
In our above example, we can call the GetLabelsOfLastTransDate stored procedure simply by using the following call statement –
CALL GetLabelsOfLastTransDate ('2019-12-01');
Calling the stored procedure mentioned above will result in the retrieval of the list of all label numbers whose last transaction occurred on the 1st of December 2012. Executing the above calling statement gives the following output:
Below is the syntax for calling or executing the stored procedure:
CALL name_of_SP (value of parameters accepted by stored procedure);
In our above example, we can call the GetLabelsOfLastTransDate stored procedure simply by using the following call statement –
CALL GetLabelsOfLastTransDate ('2019-12-01');
Output:
The stored procedure call mentioned above will return the list of all label numbers whose last transaction occurred on the 1st of December 2012. Executing the above calling statement gives the following output:
Dropping a Stored Procedure in MySQL
If we want to delete the stored procedure, then we can drop it using the following syntax:
DROP procedure [ IF EXISTS ] name_of_SP;
IF EXISTS is the optional statement that can be used to avoid raising the error when no stored procedure with name_of_SP exists in your database and you are trying to delete it.
In our above example, we can drop our GetLabelsOfLastTransDate stored procedure using the following statement –
DROP PROCEDURE GetLabelsOfLastTransDate;
Output:
Pros & Cons of using Stored Procedures in MYSQL
The main advantages of using stored procedures include the reduction of network traffic. When you transmit only the name of the stored procedure and the parameter values instead of sending multiple lengthy query statements, using stored procedures reduces network traffic. Additionally, stored procedures allow for sharing the same business logic among various applications, promoting code reusability and consistency. Besides this, the database administrator can set the privileges to specific users to alter, access, call, and drop the stored procedures.
However, there are some disadvantages to using stored procedures. One drawback is that troubleshooting and debugging the stored procedure becomes challenging, and excessive resource usage, such as CPU, may occur when employing multiple logical operations in MYSQL stored procedures. MYSQL’s efficiency in handling logical operations is not optimal.
Conclusion
We can use stored procedures to execute the subroutine that involves many database operations and a single purpose. Using stored procedures in MySQL allows the utilization of tables from multiple databases in queries. The benefits of employing stored procedures surpass situations with minimal involvement of logical operations but an abundance of database manipulation queries in your program. They are easy to call and execute.
Recommended Articles
We hope that this EDUCBA information on “MySQL Stored Procedure” was beneficial to you. You can view EDUCBA’s recommended articles for more information.