In this tutorial, we will talk about how we can put into practice a MySQL loop in a method.
For this tutorial, we presume you are conscious of how to get the job done with the MySQL treatments.
Features of MySQL Loop
Ahead of we dive into the “how” of utilizing a MySQL loop, let’s checklist a number of loop attributes:
- A loop can include much more than one particular statement in which each statement is terminated with a semicolon.
- All the statements and values within the loop are continuously executed, but only if the set issue is correct.
- You can terminate a loop by applying the Go away assertion.
- MySQL will allow you to give a identify to the loop applying the syntax loopName: LOOP
The basic syntax for applying a straightforward MySQL loop is:
Finish LOOP [end_label]
The statement list should really also involve a Go away condition that specifies when the loop should terminate.
The label represents the identify of the loop.
The syntax under reveals the loop implementation with a Leave statement:
IF ailment THEN
Go away [label]
Conclude LOOP [end_label]
Case in point Use Circumstance
In this section, I will try to illustrate how to use a LOOP in a course of action. The objective of the process is to employ a easy loop and will not replicate true-globe info.
Think about the process below that implements a loop to locate even figures:
At the time we call the loop, we get the values of even figures divided by spaces as specified in the CONCAT() perform.