WHILE loop enables to execute any SQL statement or block of statement repeatedly as long as the specified condition is true. The execution can be controlled by two keywords BREAK and CONTINUE by putting inside the loop as per user's need.
BREAK keyword: This keyword is use to stop execution if user's specified condition got true.
CONTINUE keyword: This keyword is use to skip execution of below statement. User can specify condition as per need.
Example1: WHILE loop
Example2: WHILE loop with BREAK
Example3: WHILE loop with CONTINUE
BREAK keyword: This keyword is use to stop execution if user's specified condition got true.
CONTINUE keyword: This keyword is use to skip execution of below statement. User can specify condition as per need.
Example1: WHILE loop
DECLARE @val INT = 100;
WHILE (@val <= 500)
BEGIN
PRINT @val
SET @val = @val + 100
END
ResultSet:
100
200
300
400
500
DECLARE @val INT = 100;
WHILE (@val <= 500)
BEGIN
PRINT @val
SET @val = @val + 100
IF @val>350
BREAK;
END
ResultSet:
100
200
300
DECLARE @val INT = 100;
WHILE (@val <= 500)
BEGIN
PRINT @val
SET @val = @val + 100
CONTINUE;
IF @val>350
BREAK;
END
ResultSet:
100
200
300
400
500
No comments:
Post a Comment