Get startedGet started for free

Creating a WHILE loop

In this exercise, you will use the variable you created in the previous exercise you write a WHILE loop. Recall that structure:

WHILE some_condition 

BEGIN 
    -- Perform some operation here
END

This exercise is part of the course

Intermediate SQL Server

View Course

Exercise instructions

Write a WHILE loop that increments counter by 1 until counter is less than 30.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

DECLARE @counter INT 
SET @counter = 20

-- Create a loop
___

-- Loop code starting point
___
	SELECT @counter = @counter + 1
-- Loop finish
___

-- Check the value of the variable
SELECT @counter
Edit and Run Code