Hello guys, here goes the third article of this series. If you are not sure about this article series then I would recommonded you to start from the first article in this series. Now let’s have quick flashback of the past articles.
In first article we have covers basics of the stored procedure which you can find here. And in second article we have covered the Parameters in stored procedure which are IN, OUT and INOUT. You can have this article to know more about this here.
In this today’s article we will see Conditional Controls in MySQL Stored Procedure. MySQL has two types of Conditinal Controls for Stored Procedure. Which are listed as below:
Main porpose of these conditional control is to execute query based on the value of the parameter. It will check for the value and if expression is true then it will execute the command.
Now let’s dig into each of the Conditional Controls.
IF behaves same as in other programming languages. Basic logic for IF is as below:
Now let’s see how we can use IF in MySQL Stored Procedure.
There are three different ways in which we can write IF statement using ELSE and ELSEIF, have a look at the below code block for the same.
We can also perform the nesting of the IF inside the IF but its totally based on the requirements. :)
CASE can be used when you have large number of possibility in that case it is not good to use the IF statement so CASE is the best way to handle those conditions. Have a look at the below code block for the same.
One of the main benefit of using CASE is that, it will make your code clean and easily understandable rather than complex loop nesting.
CASE makes your code clean.
So now we are done with the third article and Basic of MySQL Stored Procedure, Parameters in MySQL Stored Procedure and Conditional Controls in MySQL stored procedure. Hope you are enjoying these article series. Share your thoughts on this article series by commenting here. Also you can share and/or request a article any time.
Avinash Zala is leading various projects which deals with the various technology involved with the web. A combination of perfect technical and management skills. Avinash would like to chat with you and convert your imagination into the working system. You can get in touch with him on Facebook and Twitter.
View all posts by: Avinash