Professional Documents
Culture Documents
© Belgium Campus 2021
© Belgium Campus 2021
© Belgium Campus 2021
©©BELGIUM
BELGIUMCAMPUS
CAMPUS2021
2021
2
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.
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.
@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)
Exit
@Counter
False
<= 10?
True
@Counter
True BREAK Exit
>= 7?
False
@Counter
False
<= 20?
True
Exit
False
BEGIN
SELECT @CountryName = CountryName
FROM SampleTable WHERE Id = @Counter