Loops in SQL Part – 2

A loop that executes an unlimited number of times. It encloses a sequence of statements between the keywords LOOP and END LOOP . … If and only if the value of this expression is TRUE , the statements after LOOP execute. cursor_for_loop_statement. Issues a SQL query and loops through the rows in the result set.
Writing Loops in SQL. In programming, a loop allows you to write a set of code that will run repeatedly within the same program. Many programming languages have several different types of loop to choose from, but in SQL Server there is only one: the WHILE loop
TYPES OF LOOPS
1 SIMPLE LOOPS
2 CURSOR FOR LOOPS
3 WHILE LOOPS
4 NUMARIC FOR LOOPS
A loop that executes an unlimited number of times. It encloses a sequence of statements between the keywords LOOP and END LOOP . With each iteration, the sequence of statements is executed, then control resumes at the top of the loop. An EXIT , GOTO , or RAISE statement branches out of the loop.
At each iteration, the sequence of statements is executed and then control resumes at the top of the loop. 2. PL/SQL WHILE LOOP. Repeats a statement or group of statements while a given condition is true. It tests the condition before executing the loop body.
Variables in SQL procedures (DECLARE, SET statements) … Values can be assigned to variables using the SET statement or the SELECT INTO statement or as a default value when the variable is declared. Literals, expressions, the result of a query, and special register values can be assigned to variables.
The WHILE loop test at the start of the loop and it the condition is false, the loop code is never executed. … This is referred to as a REPEAT-UNTIL or DO-WHILE loop. PL/SQL does not directly implement a REPEAT-UNTIL loop however it is easy to construct one using an endless loop.
The init step is executed first, and only once. This step allows you to declare and initialize any loop control variables. … If it is true, the loop executes and the process repeats itself (body of loop, then increment step, and then again condition). After the condition becomes false, the ‘for’ loop terminates.

source