What is the syntax of a for
loop in TSQL?
The counter is always incremented by 1 and once the counter reaches the value of end integer, the loop ends. Syntax of for loop: FOR counter IN initial_value .. final_value LOOP.
In SQL Server, a loop is the technique where a set of SQL statements are executed repeatedly until a condition is met. SQL Server supports the WHILE loop. The execution of the statements can be controlled from within the WHLE block using BREAK and CONTINUE keywords.
There is no FOR in SQL, But you can use WHILE or GOTO to achieve the way how the FOR will work. I always prefer WHILE over GOTO statement. Show activity on this post. For loop is not officially supported yet by SQL server.
PL/SQL provides four kinds of loop statements: basic loop, WHILE loop, FOR loop, and cursor FOR loop.
There is no for-loop, only the while-loop:
DECLARE @i int = 0 WHILE @i < 20 BEGIN SET @i = @i + 1 /* do some work */ END
T-SQL doesn't have a FOR
loop, it has a WHILE
loop
WHILE (Transact-SQL)
WHILE Boolean_expression BEGIN END
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With