Powered by Blogger.

Saturday, February 15, 2014

While loop in SQL Server 2008

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:


SET @flag = 0

WHILE @flag <= 5


SET @flag = @flag + 1

PRINT 'You are in ' + CAST (@flag AS NVARCHAR(10))  +' Loop'


The out put will come as shown in the figure below.