Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

MySQL Stored Procedure

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 database queries that are executed. These queries can be executed on a conditional basis and also looped around using the FOR loop statement. Most of the time, the MySQL stored procedure is used when your application involves certain 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 certain stored procedures. These stored procedures can be called as many times as you want. 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 the subroutine i.e a procedure that can be created in MYSQL by 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: It is the name of the stored procedure that needs to be created in MySQL.

2. NameOfParameter: We can pass the optional parameters to the stored procedures that need to 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: These types of parameters are assigned the values while calling the stored procedure and the value cannot be modified or overwritten inside the stored procedure but only referenced and used by the stored procedure.
  • OUT: These are the parameters that can be assigned the values and overridden in the stored procedure but cannot be referenced by it.
  • IN OUT: These types of parameters are assigned the values while calling the stored procedure and the value can be modified or overwritten inside the stored procedure as well as 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 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 stored procedure that can contain conditional, looping statements, initializing and assigning the value of variables, and preparing and executing the database queries. Calls to other stored procedures can also be included in this part of the stored procedure.

Example to Implement MySQL Stored Procedure

Below are the examples of MySQL Stored Procedure:

1. Let us create one simple stored procedure but 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, we will create a stored procedure that will return the label numbers whose last transaction date is passed as the 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:

The syntax of calling or executing the stored procedure is shown below –

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');

The above call to the stored procedure will return the list of all the label numbers whose last transaction was done on the 1st of December of the 2012 year. Executing the above calling statement gives the following output:

The syntax of calling or executing the stored procedure is shown below:

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 above call to the stored procedure will return the list of all the label numbers whose last transaction was done on the 1st of December of the 2012 year. 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 simply 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 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 Procedure in MYSQL

The main advantages of using the stored procedure are that it reduces the traffic in the network as only the name of the stored procedure al the values of the parameter needs to be sent from application server to database server instead of sending multiple long query statements and the sharing of the same business logic written in stored procedures by multiple applications. Besides this, the database administrator can set the privileges to certain users to alter, access, call, and drop the stored procedures.

However, there are some disadvantages of using stored procedures one of them is it became hard to troubleshoot and debug the stored procedure and the resources such as CPU might get overused when using many logical operations in MYSQL stored procedures as logical operations are not designed that efficiently in MYSQL.

Conclusion

We can make the use of stored procedures to execute the subroutine that involves many database operations and involve a single purpose. The tables from multiple databases can also be used in queries of MYSQL stored procedures. The advantages of using the stored procedures certainly weigh out more when many logical operations are not involved and more of the database manipulation queries are involved in your program. They are easy to call and execute.

Recommended Articles

This is a guide to MySQL Stored Procedure. Here we discuss the Introduction of MySQL Stored Procedure and the practical examples and different subquery expressions. You can also go through our suggested articles to learn more –

  1. Introduction to MySQL Operators
  2. Top 23 MySQL String functions
  3. MySQL vs SQLite | Top 14 Comparisons
  4. Guide to MySQL Timestamp

The post MySQL Stored Procedure appeared first on EDUCBA.



This post first appeared on Free Online CFA Calculator Training Course | EduCB, please read the originial post: here

Share the post

MySQL Stored Procedure

×

Subscribe to Free Online Cfa Calculator Training Course | Educb

Get updates delivered right to your inbox!

Thank you for your subscription

×