BLOG

SQL CONTINUE

22-08-2019  0 Comment(s)

The SQL Continue statement is very useful to control the flow of a SQL While loop. Generally, we use this statement inside the While loop, and if the execution finds the SQL continue statement inside the While loop, it will stop executing the current loop iteration and starts the new iteration from the beginning.

For example, If we have 15 statements inside the loop and we want to skip executing few statements (statement 2 — statement 6, and statement 11 — statement 14) when certain condition is True otherwise, it has to execute all the 15 statements inside the loop. In this situation we can place the SQL IF ELSE or SQL ELSE IF to check for the condition, and within the if block place the continue statement in Sql Server. If the condition is True, it will stop executing statement 2 to 6, and 11 to 14 otherwise, it will execute statements from 1 to 15.

-- SQL CONTINUE Statement Example
DECLARE @Val INT
SET @Val = 1

WHILE @Val <= 10
	BEGIN
		IF (@Val = 3 OR @Val = 7)
		BEGIN
			PRINT 'Skipped By Continue Statement = ' + CONVERT(VARCHAR, @Val)
			SET @Val= @Val + 1
			CONTINUE
		END
		PRINT 'Tutorial Gateway Views = ' + CONVERT(VARCHAR, @Val)
		SET @Val= @Val + 1
	END
PRINT 'This statement is Coming from Outside the While Loop'
GO

 

 

 

Comment Here

Comments

No Comments to Show

WE ALWAYS WORK WITH :