Learn While loop in SQL

1
2637
SQL –Loop Statement

SQL –Loop Statement

The looping statement repeatedly executes command in its block until the specified condition become true or it terminates by break keyword.
SQL extensively use While loop statement to iterate the PL/SQL statement because this is the only looping statement support by SQL.
IN this article we are going to learn to how to use of While Loop, Break Keyword, and Continue keyword in sql.

While Loop: The statement in code block execute repeatedly until condition specified in Boolean expression become satisfied or it terminate by break keyword. Here is the basic syntax of while loop.

Example: This example show Multiplication of tables. I will increment the count from 1 to 10 and then do multiplication of count with specific number. Let’s say table of 2

The above looping statement gives below results

Break-Keyword: It stops the execution of while loop or it terminates the execution process of while loop

Example: in this example I am terminating the execution of statement when count values reach to 5

According the above looping statement it gives following result:

Continue- keyword: It skips the execution of all the statement after its execution and start execution from first statement of while loop.

This will give following result after execution

Example : In this example I am going to update the salary table . I will update the basic pay to increment 2000 to each employee as per bonus
Let first look on the salary table:

image1
We will make loop to update basic pay as 2000 increment for each employee.

After execution of above looping statement , we can see that basic pay is hike with 2000 for each employee
output
Hence, we have learn the While loop in SQL.

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here