Click here to Skip to main content
15,889,622 members
Articles / Programming Languages / T-SQL
Technical Blog

WHILE Loops in T-SQL Stored Procedures

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
13 Mar 2019MIT3 min read 4.1K   4   1
In this lesson, we’re going to learn about the While Loop. So, what is a While Loop? Well, While Loop is set up using a While statement. While statements are used to repeatedly execute a block of SQL statements.

In this lesson, we’re going to learn about the While Loop. So, what is a While Loop? Well, While Loop is set up using a While statement. While statements are used to repeatedly execute a block of SQL statements. That’s where the Loop phrase comes from because we’re kind of going around and around in a loop as we execute the statements.

After watching this video you’ll be able to declare WHILE loops and understand how they are used in repeating patterns within stored procedures.  You’ll find out isn’t as hard as everyone makes them out to be and you’ll have one of the skills a junior DBA should know.

If you want to learn more about how to define WHILE loops, why not take my course Stored Procedures Unpacked?  Use coupon code BLOGPOST-20 to get it at a significant discount.

tsql variables and Stored Procedures
Stored Procedures Unpacked

While Loops in T-SQL

In this lesson, we’re going to learn about the WHILE Loop. So, what is a WHILE Loop? Well, While Loop is set up using a While statement.

While statements are used to repeatedly execute a block of SQL statements. That’s where the Loop phrase comes from because we’re kind of going around and around in a loop as we execute the statements.

What is a WHILE loop?

The block is repeatedly executed while the statement’s condition is true. That’s kind of how the semantics works. While the condition of the WHILE statement is true we will execute a block of code.

Here’s an example of a WHILE Loop where we’re counting to 10. In this example we have a variable where we’re declaring it as an integer. We’re  starting at a one.

We have a While statement and it’s saying WHILE, our variable I is less than or equal to 10.

WHILE Loop Count to 10

So that’s where we’re counting the 10. We get to 10, we’re going to stop. In the meantime we have a loop and we’re going to execute this block of code within the begin and end statement.

First of all we’re going to print the code. Then we’re going to increment our variable by one. So we’re going to get, if @i is one we’re going to add one to it and it’s going to become two. And then we’re going to go back to the top.

We’re going to test @i.

Is @i less than 10?  Sure is.

Print out the value, increment the value. Keep going around and around. Before you know it we’ve added one to @i enough times that @i is going to be 11 and 11 is greater than 10.

We will not execute any more code within the beginning and end code block here and we will exit the WHILE loop.

WHILE loops are great for eliminating repeating patterns

If you find yourself repeating statements, especially those that fall into a pattern, then there’s a possibility you can use a WHILE statement to save some typing and make your programming more fun to write.

To go back and look at our example here, you can imagine having to type out this set equals I equals I plus one, 10 different times, it would be become kind of boring.

You wouldn’t do that probably but you get the point that if you have a lot of code that’s repeating, there’s a pattern to it, you’re probably going to start going, “I bet you I can do a loop and this will be a little more fun to write than just typing all this stuff out.” So when you start seeing a pattern to your code, start thinking about loops especially WHILE loops. So let’s go look at some examples of how we can use WHILE loops.


If you want to learn more about how to create stored procedures, why not take my course Stored Procedures Unpacked?  Use coupon code BLOGPOST-20 to get it at a significant discount.Image

tsql variables
Stored Procedures Unpacked

Other available programs and classes

The post WHILE Loops in T-SQL Stored Procedures appeared first on Essential SQL.

License

This article, along with any associated source code and files, is licensed under The MIT License


Written By
Easy Computer Academy, LLC
United States United States
Hello my name is Kris. I’m here because I am passionate about helping non-techie people to overcome their fear of learning SQL.

I know what it is like to not know where to start or whether the time spent learning is worth the effort. That is why I am here to help you to:
- Get started in an easy to follow step-by-step manner.
- Use your time wisely so you focus on what is important to learn to get the most value from your time.
- Answer your questions. Really! Just post a comment and I’ll respond. I’m here to help.

It wasn’t long ago that I was helping a colleague with some reporting. She didn’t know where to start and soon got overwhelmed and lost as she didn’t know SQL.

I felt really bad, as she was under pressure to get some summary information to her boss, the built-in reports were falling short, and to make them better would require her to know SQL. At that time that seemed impossible! It in dawned on me, it doesn’t have to be that way.

Then I discovered a way for anyone with the desire to easily learn SQL. I worked with my co-worker, started to teach her what I learned and soon she was able to write reports and answer her boss’ questions without getting stressed or ploughing hours into manipulating data in Excel.

It hasn’t always been easy. Sometimes the information seems abstract or too conceptual. In this case I’ve found out that a visual explanation is best. I really like to use diagrams or videos to explain hard-to-grasp ideas.

Having video, pictures, and text really help to reinforce the point and enable learning.

And now I want to help you get the same results.

The first step is simple, click here http://www.essentialsql.com/get-started-with-sql-server/

Comments and Discussions

 
GeneralWHILE loops defeat parallelism in T-SQL Pin
Jalapeno Bob15-Mar-19 7:44
professionalJalapeno Bob15-Mar-19 7:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.