Guruji Point - Code You Want To Write

  • Home
  • Asp.Net
  • WCF
  • SQL
  • Type Script
  • AngularJS
  • Tech News
  • Blog
  • Earn Online

Friday, 14 April 2017

Nested While Loop In SQL Server With Print Table Example

 asp.net, C#, example, for loop in sql, nested for loop, nested while loop, print table in sql, SQL, stored procedure, tables, while in sql     No comments   


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.


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




  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Email ThisBlogThis!Share to XShare to Facebook
Newer Post Older Post Home

0 comments:

Post a Comment

Facebook Updates

Guruji Point

Categories

Comman table Expression in SQL Dynamic Grid Row IQueryable in c# Learn AngularJS Scope_Identity() Use of indexes comma seperated string in sql row_number() and dense_rank() salary table structure. while loop in sql why do we need cursor why tuple used

About Us

This blog is about the Programming ,Tech News and some intresting facts related Contents. Here you can find fundamental and expert level topic of programming languages very practically related to C# , Asp.Net ,Sql-Server, JavaScript, Jquery, WebServices And also Web-Api, WCF ,WPF, Angular Js.

Contact Us

Email Us - gurujipoints@gmail.com
Contact- 8077657477

Reach Us

Copyright © Guruji Point - Code You Want To Write