Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

SQL Server Nested WHILE Loop Example

Nov 6, 2012
Introduction

Here I will explain how to use nested while loops in SQL Server with example.

Description

In previous articles I explained While Loop Example in SQL Server, Break or Exit while loop in SQL Server, Convert Rows to Columns in SQL, Joins in SQL Server and many articles relating to SQL Server. Now I will explain how to use nested while loops in SQL Server.

To use while loop in stored procedure we need to write the query like this


DECLARE @loop INT
SET @loop = 0
WHILE @loop <= 10
BEGIN   
SET @loop = @loop + 1   
PRINT @loop
END
If we run above query we will get output like as shown below

Output

If we want to Nested while loops we need to write the query like this


DECLARE @loop1 INT
DECLARE @loop2 INT
SET @loop1 = 1
WHILE @loop1 <= 3
BEGIN   
SET @loop2 = 1   
WHILE @loop2 <= 2   
BEGIN       
PRINT CONVERT(VARCHAR, @loop1) + ' * ' + CONVERT(VARCHAR, @loop2)+ ' = ' + CONVERT(VARCHAR, @loop1 * @loop2)       
SET @loop2 = @loop2 + 1   
END   
SET @loop1 = @loop1 + 1
END
If we run above query we will get output like as shown below

Output


If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

3 comments :

Unknown said...

nice blogs

Bharti said...

one more good website interviewqsn.com and getproductprice.com

Bharti said...

Dot Net Questions

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.