Skip to content Skip to sidebar Skip to footer

How to Create a Visual While Reading?

SQL WHILE loop provides us with the advantage to execute the SQL statement(s) repeatedly until the specified condition result plow out to be false.

In the following sections of this commodity, we volition use more flowcharts in order to explain the notions and examples. For this reason, firstly, we will explain what is a flowchart briefly. The flowchart is a visual geometric symbol that helps to explain algorithms visually. The flowchart is used to simply pattern and document the algorithms. In the flowchart, each geometric symbol specifies different meanings.

The following flowchart explains the essential structure of the WHILE loop in SQL:

Flow chart of the SQL WHILE loop

Every bit you can see, in each iteration of the loop, the defined condition is checked, then, co-ordinate to the result of the condition, the code flow is determined. If the issue of the status is true, the SQL statement will be executed. Otherwise, the code flow will exit the loop. If any SQL statement exists outside the loop, it volition be executed.

SQL WHILE loop syntax and instance

The syntax of the WHILE loop in SQL looks like equally follows:

Afterward these explanations, nosotros will give a very elementary example of a WHILE loop in SQL. In the example given below, the WHILE loop instance will write a value of the variable x times, and and then the loop will be completed:

Result of the WHILE loop example in SQL Server

At present, we will handle the WHILE loop instance line by line and examine it with details.

In this function of the code, we declare a variable, and we assign an initializing value to it:

This part of the lawmaking has a specified condition that until the variable value reaches till 10, the loop continues and executes the PRINT statement. Otherwise, the while condition will not occur, and the loop will stop:

In this last part of the code, we executed the SQL statement, and then we incremented the value of the variable:

The following flowchart illustrates this WHILE loop example visually:

SQL WHILE loop example flowchart

Infinite SQL WHILE loop

In the infinite loop AKA endless loop, the condition issue will never be false, so the loop never ends and can piece of work forever. Imagine that we accept a WHILE loop, and nosotros don't increase the value of the variable. In this scenario, the loop runs endlessly and never ends. Now, we will realize this scenario with the help of the following example. We need to accept account of one affair that we should not forget to cancel the execution of the query manually:

Infinite SQL WHILE loop  demonstration

In the following flowchart, information technology is obvious that the value of the variable never changes; therefore, the loop never ends. The reason for this issue is that the variable is ever equal to ane so the condition returns true for each iteration of the loop:

Infinite SQL WHILE loop  flowchart

BREAK statement

BREAK statement is used in the SQL WHILE loop in lodge to leave the electric current iteration of the loop immediately when certain conditions occur. In the more often than not IF…ELSE statement is used to check whether the condition has occurred or not. Refer to the SQL IF Statement introduction and overview article for more details about the IF…ELSE statement.

The following case shows the usage of the Suspension statement in the WHILE loop:

WHILE loop demonstration with BREAK statement

In this case, nosotros have checked the value of the variable, and when the value is equal or greater than 7, the code entered the IF…ELSE block and executed the Intermission statement, and so it exited the loop immediately. For this reason, the bulletin shows the values of the variable up to seven. If the condition of the IF…ELSE statement does non meet, the loop will run until the condition result volition exist imitation. The post-obit flowchart explains the working logic of the BREAK argument example as visually:

Flow chart of the SQL WHILE loop with BREAK statement

Go on statement

Go along statement is used in the SQL WHILE loop in order to stop the current iteration of the loop when certain weather condition occur, and so information technology starts a new iteration from the beginning of the loop. Assume that nosotros desire to write only even numbers in a WHILE loop. In gild to overcome this outcome, we can use the CONTINUE statement. In the following example, nosotros will check whether the variable value is odd or even. If the variable value is odd, the lawmaking enters the IF…ELSE statement blocks and increase the value of the variable, execute the Go on statement and starts a new iteration:

WHILE loop demonstration with CONTINUE statement

The following flowchart explains the working logic of the Proceed statement case as visually:

Flow chart of the SQL While loop with CONTINUE statement

Reading table records through the WHILE loop

In the post-obit example, we volition read tabular array information, row by row. Firstly we will create a sample table:

SampleTable image

In this step, we will read all data row past row with the help of the WHILE loop:

Reading  table records with SQL WHILE loop

In this example, we read the table rows via the WHILE loop. We tin can besides develop more sophisticated and advanced loops based on our needs.

Conclusion

In this article, nosotros learned the SQL WHILE loop with quite unproblematic examples. We also virtualized and explained the examples with flowcharts. WHILE loop helps u.s. to achieve iterative operations in SQL Server. At the same time, BREAK and Keep statements can be used to command iteration of the WHILE loop in SQL Server.

  • Author
  • Contempo Posts

Esat Erkec

carrollgrevinat.blogspot.com

Source: https://www.sqlshack.com/sql-while-loop-with-simple-examples/

Post a Comment for "How to Create a Visual While Reading?"