Syntax of for-loop in SQL Server
Extra Info
Just to add as no-one has posted an answer that includes how to actually iterate over a dataset inside a loop. You can use the keywords OFFSET FETCH.
Usage
DECLARE @i INT = 0;
SELECT @count= Count(*) FROM {TABLE}
WHILE @i <= @count
BEGIN
SELECT * FROM {TABLE}
ORDER BY {COLUMN}
OFFSET @i ROWS
FETCH NEXT 1 ROWS ONLY
SET @i = @i + 1;
END
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