Here I will explain how to use while loop in SQL Server stored procedure with example.
In previous articles I explained Find First and last day of current month in SQL, Convert Rows to Columns in SQL, Joins in SQL Server and many articles relating to SQL Server Now I will explain how to use while loop in SQL Server stored procedure with example.
To use while loop in stored procedure we need to write the query like this
DECLARE @loop INT
SET @loop = 0
WHILE @loop <= 10
SET @loop = @loop + 1
If we run above query we will get output like as shown below
In this way we can use while loop in SQL Server.