By creating a loop, we can perform certain operations as long as this loop continues.
We can use While Loop as follows. As long as @testvariable is less than 10, operations within the BEGIN and END blocks in the WHILE block will be executed repeatedly.
1 2 3 4 5 6 |
DECLARE @testvariable int=1 WHILE(@testvariable<10) BEGIN Select @testvariable SET @testvariable+=1; END |
To use While Loop with a Cursor, I suggest you read the article “Cursor Usage in SQL Server“.