In this post we will discuss about how to use while loop in SQL server 2008. Also you can check out my previous posts on Difference between primary key and unique key in SQL Server, Difference between primary key and foreign key in SQL server and Like operator examples in SQL Server 2008.
Below is the syntax to use While Loop in SQL Server:
DECLARE @flag INT
SET @flag = 0
WHILE @flag <= 5
BEGIN
SET @flag = @flag + 1
PRINT 'You are in ' + CAST (@flag AS NVARCHAR(10)) +' Loop'
END
The out put will come as shown in the figure below.
Below is the syntax to use While Loop in SQL Server:
DECLARE @flag INT
SET @flag = 0
WHILE @flag <= 5
BEGIN
SET @flag = @flag + 1
PRINT 'You are in ' + CAST (@flag AS NVARCHAR(10)) +' Loop'
END
The out put will come as shown in the figure below.