Introduction
In this post we will learn how to do nested looping with while loop in SQL server.
Previous Updates
In previous articles we have learnt If Else and Case statement , While Loop 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 example.Group By in SQL Server and its use.
Practice
As we learn in our previous article why and when do we need use while loop. When we need to repeat the same execution multiple time based on condition then we use while loop in Sql server.
Here in this below given example i will print a table using nested While loop. You already did the similar thing in c# code but using for loop. Now here instad of for loop we use nested while to print a table in sql.
Output
Previous Updates
In previous articles we have learnt If Else and Case statement , While Loop 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 example.Group By in SQL Server and its use.
Practice
As we learn in our previous article why and when do we need use while loop. When we need to repeat the same execution multiple time based on condition then we use while loop in Sql server.
Here in this below given example i will print a table using nested While loop. You already did the similar thing in c# code but using for loop. Now here instad of for loop we use nested while to print a table in sql.
DECLARE @firstLoop INT
DECLARE @secondLoop INT
SET @firstLoop = 5
WHILE @firstLoop <= 5
BEGIN
SET @secondLoop = 1
WHILE @secondLoop <= 5
BEGIN
PRINT CONVERT(VARCHAR, @firstLoop) + ' * ' + CONVERT(VARCHAR, @secondLoop)
+ ' = ' + CONVERT(VARCHAR, @firstLoop * @secondLoop)
SET @secondLoop = @secondLoop + 1
END
SET @firstLoop = @firstLoop + 1
END |
Output
0 comments:
Post a Comment