© Belgium Campus 2021

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

1

©©BELGIUM
BELGIUMCAMPUS
CAMPUS2021
2021
2

© BELGIUM CAMPUS 2021


3
Start

The WHILE statement repeatedly WHILE


executes one Transact-SQL statement loop False
condition
(or more, enclosed in a block) while
the Boolean expression evaluates to
true.

In other words, if the expression is


true, the statement (or block) is
executed, and then the expression is Exit from While
True
loop
evaluated again to determine if the
statement (or block) should be
executed again. This process repeats
until the expression evaluates to false.
SQL statement

© BELGIUM CAMPUS 2021


4

WHILE Boolean_expression
sql statements
The WHILE statement repeatedly
executes one Transact-SQL statement [BREAK]
(or more, enclosed in a block) while
the Boolean expression evaluates to
sql statements
true. [CONTINUE]
In other words, if the expression is
true, the statement (or block) is
executed, and then the expression is
evaluated again to determine if the
statement (or block) should be
executed again. This process repeats
until the expression evaluates to false.

© BELGIUM CAMPUS 2021


5

USE sample;
WHILE (SELECT SUM(budget)
FROM project) < 500000
The WHILE statement repeatedly BEGIN
executes one Transact-SQL statement
(or more, enclosed in a block) while UPDATE project SET budget = budget*1.1
the Boolean expression evaluates to
true.
IF (SELECT MAX(budget)
FROM project) > 240000
In other words, if the expression is
true, the statement (or block) is BREAK
executed, and then the expression is
ELSE CONTINUE
evaluated again to determine if the
statement (or block) should be END
executed again. This process repeats
until the expression evaluates to false.

© BELGIUM CAMPUS 2021


6
DECLARE @Counter INT
SET @Counter = 1

@Counter
False
<= 10?
DECLARE @Counter INT
SET @Counter = 1
WHILE @Counter<= 10
BEGIN
True
PRINT 'The counter value is = ' + CONVERT (VARCHAR,@Counter)
SET @Counter = @Counter + 1
END PRINT ‘The counter value is = ‘ + CONVERT (VARCHAR,@Counter)

SET @Counter = @Counter + 1

Exit

© BELGIUM CAMPUS 2021


7

DECLARE @Counter INT


SET @Counter=1
WHILE ( @Counter <= 10)
BEGIN
PRINT 'The counter value is = ' + CONVERT(VARCHAR,@Counter)
IF @Counter >=7
BEGIN
BREAK
END
SET @Counter = @Counter + 1
END

© BELGIUM CAMPUS 2021


8
DECLARE @Counter INT
SET @Counter = 1

@Counter
False
<= 10?

True

PRINT 'The counter value is = ' + CONVERT (VARCHAR,@Counter)

@Counter
True BREAK Exit
>= 7?

False

SET @Counter = @Counter + 1

© BELGIUM CAMPUS 2021


9

DECLARE @Counter INT


SET @Counter=1
WHILE (@Counter <= 20)
BEGIN
IF @Counter % 2 =1
BEGIN
SET @Counter = @Counter + 1
CONTINUE
END
PRINT 'The counter value is = ' + CONVERT(VARCHAR,@Counter)
SET @Counter = @Counter + 1
END

© BELGIUM CAMPUS 2021


10
DECLARE @Counter INT
SET @Counter = 1

@Counter
False
<= 20?

True

SET @Counter = @Counter + 1 @Counter


True
CONTINUE % 2 = 1?

Exit

False

PRINT 'The counter value is = ' + CONVERT(VARCHAR,@Counter)


SET @Counter = @Counter + 1

© BELGIUM CAMPUS 2021


11
USE tempdb
GO

DECLARE @Counter INT ,


@MaxId INT,
@CountryName NVARCHAR(100)
SELECT @Counter = MIN(Id) , @MaxId = MAX(Id)
FROM SampleTable

WHILE(@Counter IS NOT NULL


AND @Counter <= @MaxId)

BEGIN
SELECT @CountryName = CountryName
FROM SampleTable WHERE Id = @Counter

PRINT CONVERT(VARCHAR,@Counter) + '. country name is ' + @CountryName


SET @Counter = @Counter + 1
END

© BELGIUM CAMPUS 2021

You might also like