Introduction
In this post we will learn how to looping in SQL server with while loop. Why we need while loop in sql and how can we use while loop with Stored Procedure.
Previous Updates
In previous articles we have learnt If Else and Case statement in SQL. What is Pivot table in SQl. Difference between Scope_Identity(), @@Identity and Ident_Current . Stuff and Replace in SQl. Temp table and table variable difference and when to use. Sequence in sql server with live situation example.Group By in SQL Server and use with Aggregate Functions
When To Use
In SQL server many situations you did not know about how much time you need to execute the loop body. Using while loop you will be ensure that at a given condition your loop will iterate just like for loop in c#. Here instead of for loop you have to use while. There is no for loop in sql.
You can use Break statement whenever you want to break out from while loop.
Practice
Write this type of query to use while loop with Stored Procedure or without Procedure.
Previous Updates
In previous articles we have learnt If Else and Case statement in SQL. What is Pivot table in SQl. Difference between Scope_Identity(), @@Identity and Ident_Current . Stuff and Replace in SQl. Temp table and table variable difference and when to use. Sequence in sql server with live situation example.Group By in SQL Server and use with Aggregate Functions
When To Use
In SQL server many situations you did not know about how much time you need to execute the loop body. Using while loop you will be ensure that at a given condition your loop will iterate just like for loop in c#. Here instead of for loop you have to use while. There is no for loop in sql.
You can use Break statement whenever you want to break out from while loop.
Practice
Write this type of query to use while loop with Stored Procedure or without Procedure.
DECLARE @loopDay INT
SET @loopDay = 5
WHILE @loopDay <= 10
BEGIN
SET @loopDay = @loopDay + 1
PRINT @loopDay
END
|
Output
0 comments:
Post a Comment