DECLARE @Total INT = 0;
@Number WHILE <= 10
BEGIN
IF @NUMBER = 5
BREAK;
ELSE
SET @Total = @Total + @Number;
SET @Number = @Number + 1;
END
PRINT @Total;
GO
In this example using the BREAK statement, the WHILE loop will end when @Number reaches value 5.
Previous article: FOR loop in SQL Server
Next lesson: CONTINUE command in SQL Server