Learn how to simulate the FOR LOOP in SQL Server (Transact-SQL) with syntax and examples.
In SQL Server, there is no FOR LOOP. However, you simulate the FOR LOOP using the WHILE LOOP.
The syntax to simulate the FOR Loop in SQL Server (Transact-SQL) is:
The number of times that you want the simulated FOR LOOP (ie: WHILE LOOP) to execute.
The statements of code to execute each pass through the loop.
Let's look at an example that shows how to simulate the FOR LOOP in SQL Server (Transact-SQL) using the WHILE LOOP.
For example:
In this WHILE LOOP example, the loop would terminate once @cnt reaches 10.