ITERATE
ITERATE is a statement in SQL, generally used in structured Dynamic SQL. It is typically used in loops and is comparable to the continue statement in programming languages such as Java or C++. When executed, the ITERATE statement skips the remaining queries in the current iteration of the loop and moves control to the next iteration of the loop.
Example
Output
Explanation
This is a simple example of the ITERATE statement in MySQL. The code initializes a variable @x to 0 and creates a loop that increments @x by one and prints it out. When @x reaches 10, it exits the loop. The output displays the values of @x as they are updated, from 1 to 10.
Example
Output
Explanation
In this example, a loop is created using a WHILE statement, which adds the value of @Count
variable to the @Sum
variable. When the value of @Count
reaches 5, the CONTINUE statement is triggered, exiting the current cycle and skipping to the next iteration of the loop. This specific scenario is similar to an ITERATE statement in other SQL systems. Since SQL Server doesn’t support ITERATE keyword, CONTINUE is used to simulate its functionality. The output ‘50’ is the total sum of numbers from 1 to 10.
Example
Output
Explanation
This PL/SQL block initializes a looping structure using the LOOP..END LOOP syntax in Oracle. The counter variable is initialized to 1. Inside the loop, the counter value is outputted then increased by 1. Once the counter is greater than 3, the LOOP is exited.