FOR loop in SQL Server

The FOR loop is often used to run a code repeatedly for the number of repetitions. However, in SQL Server there is no FOR loop. However, you can still use the WHILE loop to simulate FOR here. Please follow Quantrimang to see how to use it through the following article.

For loop syntax in SQL Server

To simulate the FOR loop in SQL Server, we use the following syntax:

 DECLARE @cnt INT = 0; 

WHILE @cnt < cnt_total
BEGIN
{.câu lệnh thực thi.}
SET @cnt = @cnt + 1;
END;

Parameters :

  1. cnt_total: Number of loops you want FOR (WHILE) to execute.
  2. Executable statements: Statements are executed each time through the loop.

Example of a for loop in SQL Server

Consider an example of how to simulate a FOR loop in SQL Server (Transact-SQL) with a WHILE loop.

 DECLARE @cnt INT = 0; 

WHILE @cnt < 10
BEGIN
PRINT 'Vi du mo phong vong lap FOR trong SQL Server cua TipsMake.com';
SET @cnt = @cnt + 1;
END;

PRINT 'Ket thuc mo phong vong lap FOR cua TipsMake.com';
GO

In the example using this WHILE command, the loop will terminate when @cnt reaches the value of 10.

Last lesson: WHILE loop in SQL Server

Next article: BREAK (Control Disconnect) command in SQL Server

4 ★ | 1 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.