Hi all, Here we reach to the Loops in MySQL Stored Procedure. But before starting, I hope you are aware of previous articles. If not let me give falshback first.
Upto now we have covered the basic Introduction For Stored Procedure in MySQL, then we have compeleted the Types of Parameter in MySQL Stored Procedure. After parameter we have covered the Conditional Controls in MySQL Stored Procedure.
Now I am going to covers the different types of Loops in MySQL Stored Procedure.
We can have three diffrent types of Loops in MySQL Stored Procedure, which are as below:
Let’s dive into each one by one :)
This loop also known as POST-TEST loop as this loop will execute the statement first then check for the condition. This loop will keep executing untill the conditions gets false. Here is the basic syntax for this loop:
Here is the basic demo for how you can use this loop in your MySQL Stored Procedure.
This is reverse of the WHILE loop, as this loop will check the condition first before executing the statement. Due to this reason, this loop is also known as PRE-TEST loop in MySQL Stored Procedure.
Let’s have a look at basic syntax of this loop:
Here is the basic demo for this WHILE loop in MySQL Stored Procedure.
This loop is similar to break and continue in other programming lanaguage. LOOP will behave as a body of the loop, while LEAVE is used to leave out of the loop and ITERATE is used to continue with the loop.
Let’s have a look at the sample procedure which uses the LOOP, LEAVE and ITERATE in it.
Now we are done with the all three types of loops in MySQL Stored Procedure. Now in upcoming article we will cover Transaction (Rollback & Commit) in Stored Procedure. Subscribe to our RSS Feed to don’t miss that article. And Don’t forget to follow us on Twiiter and to Like us on Facebook.
Avinash Zala currently working at a leading web development company as a Sr. PHP Web Developer. He is the founder of the Xpert Developer. He has completed B.E. in Information Technology. Born and brought up in Ahmedabad. He loves to code and coding is all about passion for him. Follow him on twitter at Avinash Zala & Xpert Developer.
View all posts by: Avinash