562 attributed reviews in the last 3 years
Refreshingly small course sizes
Outstandingly good courseware
Whizzy online classrooms
Wise Owls only (no freelancers)
Almost no cancellations
We have genuine integrity
We invoice after training
Review 30+ years of Wise Owl
View our top 100 clients
Search our website
We also send out useful tips in a monthly email newsletter ...
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!
This blog is part of a complete SQL tutorial. We also run classroom-based SQL training courses. |
When you've written the code to begin a loop you can add further conditions to make the loop end prematurely.
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
'The first group with 0 films is ' +
CAST(@Counter AS VARCHAR(2)) + ' Oscars'
--Exit from the loop
BREAK
END
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 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
)
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:
Click here, or press ALT + BREAK on the keyboard.
Once you've managed to break into your code you can then go about ensuring that the loop has an end point before running it again!
Hopefully we'll see more useful results after we've tweaked the code.
Parts of this blog |
---|
|
Some other pages relevant to the above blogs include:
Kingsmoor House
Railway Street
GLOSSOP
SK13 2AA
Landmark Offices
99 Bishopsgate
LONDON
EC2M 3XD
Holiday Inn
25 Aytoun Street
MANCHESTER
M1 3AE
© Wise Owl Business Solutions Ltd 2023. All Rights Reserved.