Stored Procedure In SQL

Stored Procedure In SQL

What is Stored Procedure?

A set of SQL queries is called a stored procedure. Whenever we need one set of SQL statements to use again and again in our database program, then we write those SQL statements together and store them inside a procedure(called Stored Procedure). The stored procedure is saved permanently in the database. It has a special syntax. It is just like a method in any language. We use a method so that we don't have to write the method definition again and again and we can call it just by its name whenever we want and use them accordingly. In the same way, we can also call Stored Procedure just by its name. And just like the method accepts parameters and can return values, a Stored procedure also accepts parameters and can return values according to its body definition.

Syntax

Stored Procedure without Parameter

DELIMITER $$

CREATE PROCEDURE <ProcedureName>

    BEGIN
sql Statements
    END$$

DELIMITER ;

Note: In MySQL, we use a CALL statement to call a stored procedure.

Learning through an example:

We have created a table named funcinemaranchi in a database name juniorg.

Now, we have created a stored procedure named selectByTheater in juniorg database and stored two SQL statements.

Now, we are calling the selectByTheater stored Procedure which should return 1st and 3rd Screen Theaters and its show. (Call Statement in MySQL is necessary to show results of stored procedures)

Stored Procedure with Parameters()

We use three types of parameters in the Stored procedure

  1. IN Parameter

  2. OUT Parameter

  3. INOUT Parameter

We basically use only in and out parameters on daily basis. So, we are going to see only those two.

Explaining one by one with examples:

we have taken the same database, table, and stored procedure that we have used in the above example. we will use in parameter to search theaters with their number.

IN PARAMETER SYNTAX(WITH EXAMPLE)

In the above picture, we have altered the existing stored procedure and used the parameter screenNo to find the theaters.

Now, calling the stored procedure (selectByTheater) with input passed as a parameter.

You can see all TheaterScreenNo with 1.

OUT PARAMETER

This parameter is used to store the results, the query has given.

For example, we have created a new Stored Procedure by the name getTotalMoviesByScreenNo.This procedure throws no of movies running on a particular Screen In the Cinema Hall by giving the screen number as in the parameter. We are using the same database as used in the above examples with the same table.

Stored procedure for out parameter:

Now, we are calling that store procedure with in parameter as 2 and storing the output in res parameter(u can give any name for the out parameter. don't forget to use @ before the out parameter). I named the out parameter as res and later changed it to noOfMovies.And at last use select query as it will show the count of movies running in the screen2.

Important: If there is more than one parameter in the stored procedure then, Give the inputs for the parameters in sequence in the call statement as u have given in the stored procedure, or if u don't want to follow the sequence of the input for a parameter then execute the call statement with parameter and argument as key-value pair .

Important: You cannot understand it properly unless writing the code yourself and get your hands dirty. So please, write the code and practice to understand this article properly. This article is mainly practically based, with very little scope of the theory. but I have tried to make you understand all the concepts with examples.

If you are enjoying the content, please subscribe to my newsletter to get more such articles and kindly sponsor my work (if you can). The subscription and sponsor link is on the bottom page of this article.

Did you find this article valuable?

Support Gaurav's In-Depth Java by becoming a sponsor. Any amount is appreciated!