Monday, 24 October 2016

SQL - While Loop Syntax

Below is the WHILE LOOP Syntax in SQL

DECLARE @cnt INT = 1;

WHILE @cnt BETWEEN 1 AND 20
BEGIN

PRINT @cnt;
SET @cnt = @cnt+1;

END

SQL Doesn't have For Loop, instead you have to use While loop.

SQL - For Loop Syntax

SQL doesn't have For Loop, instead you have to use While loop.
The syntax is as below.


DECLARE @cnt INT = 1;

WHILE @cnt BETWEEN 1 AND 20
BEGIN

PRINT @cnt;
SET @cnt = @cnt+1;

END