0% found this document useful (0 votes)
25 views

SQL SERVER - Simple Example of WHILE Loop With Break and Continue

This document provides examples of using WHILE loops with BREAK and CONTINUE statements in SQL Server. The first example prints out numbers from 1 to 10, using BREAK to exit the loop when the number is greater than 5. The second example increases product prices in a loop until the average price exceeds $300 or the maximum price exceeds $500, at which point it breaks out of the loop.

Uploaded by

Vendry Mahendra
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

SQL SERVER - Simple Example of WHILE Loop With Break and Continue

This document provides examples of using WHILE loops with BREAK and CONTINUE statements in SQL Server. The first example prints out numbers from 1 to 10, using BREAK to exit the loop when the number is greater than 5. The second example increases product prices in a loop until the average price exceeds $300 or the maximum price exceeds $500, at which point it breaks out of the loop.

Uploaded by

Vendry Mahendra
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

SQL SERVER – Simple Example of WHILE Loop with

BREAK and CONTINUE
February 4, 2008 by pinaldave

WHILE statement sets a condition for the repeated execution of an SQL statement or statement
block. Following is very simple example of WHILE Loop with BREAK and CONTINUE.
USE AdventureWorks;
GO
DECLARE @Flag INT
SET @Flag = 1
WHILE (@Flag < 10)
BEGIN
BEGIN
PRINT @Flag
SET @Flag = @Flag + 1
END
IF(@Flag > 5)
BREAK
ELSE
CONTINUE
END
WHILE loop can use SELECT queries as well. You can find following example of BOL very
useful.
USE AdventureWorks;
GO
WHILE (
SELECT AVG(ListPrice)
FROM Production.Product) < $300
BEGIN
UPDATE Production.Product
SET ListPrice = ListPrice * 2
SELECT MAX(ListPrice)
FROM Production.Product
IF (
SELECT MAX(ListPrice)
FROM Production.Product) > $500
BREAK
ELSE
CONTINUE
END
PRINT 'Too much for the market to bear

You might also like