Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
65 views

SQL WHILE Loop Syntax and Example

The WHILE loop in SQL allows executing a block of code repeatedly as long as a condition is TRUE. The basic syntax includes a condition, BEGIN and END blocks. An example shows initializing a counter variable, checking if it is less than or equal to 10, printing the counter value, and incrementing the counter each iteration. BREAK and CONTINUE statements can be used to exit or skip iterations under certain conditions. Reading table records using a WHILE loop is also demonstrated.

Uploaded by

manisha mudgal
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views

SQL WHILE Loop Syntax and Example

The WHILE loop in SQL allows executing a block of code repeatedly as long as a condition is TRUE. The basic syntax includes a condition, BEGIN and END blocks. An example shows initializing a counter variable, checking if it is less than or equal to 10, printing the counter value, and incrementing the counter each iteration. BREAK and CONTINUE statements can be used to exit or skip iterations under certain conditions. Reading table records using a WHILE loop is also demonstrated.

Uploaded by

manisha mudgal
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

SQL WHILE loop syntax and

example
The syntax of the WHILE loop in SQL looks like as follows:

WHILE condition
BEGIN
   {...statements...}
END

After these explanations, we will give a very simple example of a WHILE loop
in SQL. In the example given below, the WHILE loop example will write a value
of the variable ten times, and then the loop will be completed:
DECLARE @Counter INT
SET @Counter=1
WHILE ( @Counter <= 10)
BEGIN
    PRINT 'The counter value is = ' +
CONVERT(VARCHAR,@Counter)
    SET @Counter  = @Counter  + 1
END
Break Statement
BREAK statement is used in the SQL WHILE loop in
order to exit the current iteration of the loop
immediately when certain conditions occur. In the
generally IF…ELSE statement is used to check
whether the condition has occurred or not.
• DECLARE @Counter INT
• SET @Counter=1
• WHILE ( @Counter <= 10)
• BEGIN
•   PRINT 'The counter value is = ' +
CONVERT(VARCHAR,@Counter)
•   IF @Counter >=7
•   BEGIN
•   BREAK
•   END
Continue Statement
CONTINUE statement is used in the SQL WHILE loop in order to stop the current
iteration of the loop when certain conditions occur, and then it starts a new
iteration from the beginning of the loop. Assume that we want to write only even
numbers in a WHILE loop. In order to overcome this issue, we can use the
CONTINUE statement. In the following example, we will check whether the variable
value is odd or even. If the variable value is odd, the code enters the IF…ELSE
statement blocks and increment the value of the variable, execute the
CONTINUE statement and starts a new iteration:
CONTINUE statement

• 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
Reading table records through the WHILE loop

• CREATE TABLE Table1


• (Id INT, CountryName NVARCHAR(100))
• GO
• INSERT INTO SampleTable ( Id, CountryName)
• Values (1, 'Germany'),
•         (2, 'France'),
•         (3, 'Italy'),
•     (4, 'Netherlands') ,
•        (5, 'Poland')
•  
•  
•  
•   SELECT * FROM Table1
DECLARE @Counter INT , @MaxId INT,
        @CountryName NVARCHAR(100)
SELECT @Counter = min(Id) , @MaxId = max(Id)
FROM Table1
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

You might also like