MS SQL Control of flow - While
-- Example 1 : Simple While
DECLARE @count INT = 1;
WHILE @count <= 3
BEGIN;
PRINT 'Current row: ' + str(@count);
SET @count = @count + 1;
END;
-- [Output]
-- Current row: 1
-- Current row: 2
-- Current row: 3
-- Example 2 : While with If...Else, Break & Continue
DECLARE @Sum INT= 0,
@IncrementBy INT= 2;
WHILE @Sum < 50
BEGIN
SET @Sum += @IncrementBy;
IF @Sum > 40
BEGIN
PRINT 'Exceeded the threshold.';
BREAK;
END
ELSE
BEGIN
PRINT 'Current Sum ' + Str(@Sum);
CONTINUE;
END
END
PRINT 'Finished calculating.';