Loops in SQL
Part three of a three-part series of blogs

Many programming languages feature a variety of types of loop which allow your programs to repeat a set of instructions multiple times. In SQL there is only one type of loop, and this blog explains how it works!

1. Writing Loops in SQL
2. Running SQL Queries in a Loop
3. Ending Loops and Endless Loops (this blog)

This blog is part of a complete SQL tutorial.  We also run classroom-based SQL training courses.

Posted by Andrew Gould on 18 February 2013

You need a minimum screen resolution of about 700 pixels width to see our blogs. This is because they contain diagrams and tables which would not be viewable easily on a mobile phone or small laptop. Please use a larger tablet, notebook or desktop computer, or change your screen resolution settings.

# Ending Loops and Endless Loops

When you've written the code to begin a loop you can add further conditions to make the loop end prematurely.

## The BREAK Statement

To prematurely end a loop you can add a BREAK statement to your code.  You would normally do this within an IF statement.  In the example below, if we find that the number of films winning a certain number of Oscars falls to zero we will exit the loop:

DECLARE @Counter INT

DECLARE @MaxOscars INT

DECLARE @NumFilms INT

SET @Counter = 0

SET @MaxOscars = (SELECT MAX(FilmOscarWins) FROM tblFilm)

WHILE @Counter <= @MaxOscars

BEGIN

SET @NumFilms =

(

SELECT COUNT(*)

FROM tblFilm

WHERE FilmOscarWins = @Counter

)

--Check if the number of films is 0

IF @NumFilms = 0

BEGIN

--Print a simple message

PRINT

'The first group with 0 films is ' +

CAST(@Counter AS VARCHAR(2)) + ' Oscars'

--Exit from the loop

BREAK

END

PRINT

CAST(@NumFilms AS VARCHAR(3)) +

' films have won ' +

CAST(@Counter AS VARCHAR(2)) +

' Oscars.'

SET @Counter += 1

END

The output from this code is shown in the diagram below:

When we execute the code we find that it ends on the tenth pass through the loop, when we find that no films won 9 Oscars.

Using the BREAK statement means that you can give your loops multiple exit routes.  But what happens if you've written a loop which doesn't have any exit points at all; the so-called endless loop?

## Getting Stuck in an Endless Loop

Getting stuck in an endless loop is embarrassingly easy; all you need to do is write a loop which never finds an exit point.  In the code below we've "accidentally" forgotten to add the line which increments our loop counter:

DECLARE @Counter INT

DECLARE @MaxOscars INT

DECLARE @NumFilms INT

SET @Counter = 0

SET @MaxOscars = (SELECT MAX(FilmOscarWins) FROM tblFilm)

WHILE @Counter <= @MaxOscars

BEGIN

SET @NumFilms =

(

SELECT COUNT(*)

FROM tblFilm

WHERE FilmOscarWins = @Counter

)

PRINT

CAST(@NumFilms AS VARCHAR(3)) +

' films have won ' +

CAST(@Counter AS VARCHAR(2)) +

' Oscars.'

--We should have incremented the loop counter here

END

We can set this code running but we'll never see it reach a natural end.

The code is running, but we'll be waiting for a long time if we're expecting it to finish!

When you realise that your code is stuck in an endless loop you can attempt to force it to end by clicking the tool shown in the diagram below: