WHILE loop in SQL Server

The WHILE loop (WHILE LOOP) is used if you want to run a code repeatedly when the given condition returns TRUE. The article will explain in detail how to use it with a clear syntax and example to make it easier to imagine WHILE in SQL Server.

Syntax

To use WHILE loop in SQL Server, we use the syntax as below:

 WHILE dieukien / * commands to repeat * / 
BEGIN
{. execute command when condition is TRUE .}
END;

Note:

  1. Use the WHILE loop statement when you are unsure of the number of times you want to execute.
  2. Since the WHILE condition is evaluated before entering the loop, the loop may not work once (when dieukien is FALSE, the loop will end immediately).
  3. See the command BREAK to exit the WHILE loop soon.
  4. See also the CONTINUE statement to restart the WHILE loop from the beginning.

Chart WHILE loop

WHILE loop in SQL Server Picture 1

For example

 DECLARE @Number INT = 1; 
DECLARE @Total INT = 0;

@Number WHILE <= 10
BEGIN
SET @Total = @Total + @Number;
SET @Number = @Number + 1;
END

PRINT @Total;

GO

In this example, the loop will not perform any time if at the beginning of @Number> 10, it only executes and maintains when variable <= 10. When the condition is exceeded (> 10), the loop will end End and continue executing the next statement.

Previous article: IF . ELSE command in SQL Server

Next article: FOR loop in SQL Server

5 ★ | 2 Vote

May be interested

  • What is Loop mail?What is Loop mail?
    what is loop mail? to help you with this question, the following article will help you find out what loop mail is.
  • For ... loop in JavaScriptFor ... loop in JavaScript
    the for ... in loop is used to iterate over the properties of an object. when we haven't discussed the object yet, you may not feel comfortable with this loop. but once you understand how objects work in javascript, you will find this loop very useful.
  • Loop control in JavaScriptLoop control in JavaScript
    javascript provides high control to handle loop commands and switch commands. there may be a situation when you need to exit the loop without having to go to its endpoint. there may also be situations when you want to jump over a part of the code block and start the next loop.
  • Why Microsoft Loop Can't Compare to NotionWhy Microsoft Loop Can't Compare to Notion
    many people have been using notion for brainstorming for years, so it's interesting to see if loop can replace it as they move deeper into the microsoft ecosystem.
  • How to fix 'Please Wait for the GPSVC' loop errorHow to fix 'Please Wait for the GPSVC' loop error
    the please wait for the gpsvc loop error in windows is an annoying problem that can cause the system to get stuck at shutdown. this loop is related to group policy client service (gpsvc).
  • The while loop in ShellThe while loop in Shell
    the while loop gives you the ability to execute a set of repetitive commands until certain conditions occur. it is often used when you need to manipulate iterative variable values.
  • Until loop in ShellUntil loop in Shell
    the while loop is perfect for the situation where you want to execute a set of commands while some conditions are true. sometimes you need to execute a set of commands until a condition is true, then you need to use a until loop.
  • Select loop in ShellSelect loop in Shell
    the select loop provides an easy way to create a numbered menu from which the user can select. it is useful when you want to ask the user to select one or more items from a list of options.
  • For loop in ShellFor loop in Shell
    the for loop works on lists of items (items). it repeats a set of commands for each item in a list.
  • BREAK (Control Interrupt) command in SQL ServerBREAK (Control Interrupt) command in SQL Server
    the break command used to exit the loop does not specify a stop condition or you want to stop the loop on condition that you specify and execute the statements following the loop statement end.