国内最大的酷站演示中心!
november 5, 2003
t-sql programming part 2 - building a t-sql loop
by gregory a. larsen
this is the second article in my t-sql programming series. this article will discuss building a program loop using t-sql. in addition to talking about building a loop, i will also discuss ways of controlling the loop processing, and different methods to break out of a loop.
a programming loop is a chunk of code that is executed over and over again. in the loop some logic is executed repeatedly in an iterative fashion until some condition is met that allows the code to break out of the loop. one example of where you might use a loop would be to process through a set of records one record at a time. another example might be where you need to generate some test data and a loop would allow you to insert a record into your test data table with slightly different column values, each time the loop is executed. in this article i will discuss the while, break, continue, and goto statements.
while statement
in t-sql the while statement is the most commonly used way to execute a loop. here is the basic syntax for a while loop:
while <boolean expression> <code block>
where a <boolean expression> is any expression that equates to a true or false answer, and the <code block> is the desire code to be executed while the <boolean expression> is true. let's go through a real simple example.
in this example i will increment a counter from 1 to 10 and display the counter each time through the while loop.
declare @counter intset @counter = 0while @counter < 10begin set @counter = @counter + 1 print 'the counter is ' + cast(@counter as char)end
here the code executes the while statement as long as the @counter integer variable is less than 10, this is the boolean expression of the while loop. the @counter variable starts out at zero, and each time through the while loop it is incremented by 1. the print statement displays the value in the @counter variable each time through the while loop. the output from this sample looks like this:
the counter is 1 the counter is 2 the counter is 3 the counter is 4 the counter is 5 the counter is 6 the counter is 7 the counter is 8 the counter is 9 the counter is 10
as you can see, once the @counter variable reaches 10 the boolean expression that is controlling the while loop is no longer true, so the code within the while loop is no longer executed.
not only can you have a single while loop, but you can have while loops inside while loops. or commonly know as nesting of while loops. there are lots of different uses where nesting is valuable. i commonly use nesting of while loops to generate test data. my next example will use the while loop to generate test records for a part table. a given part record is uniquely identified by a part_id, and a category_id. for each part_id there are three different category_id's. here is my example that generates 6 unique records for my part table using a nested while loop.
declare @part_id intdeclare @category_id intdeclare @desc varchar(50)create table part (part_id int, category_id int, description varchar(50))set @part_id = 0set @category_id = 0 while @part_id < 2begin set @part_id = @part_id + 1 while @category_id < 3 begin set @category_id = @category_id + 1 set @desc = 'part_id is ' + cast(@part_id as char(1)) + ' category_id ' + cast(@category_id as char(1)) insert into part values(@part_id, @category_id, @desc ) end set @category_id = 0 endselect * from partdrop table part
here is the output from the select statement at the bottom of this nested while loop example.
part_id category_id description ----------- ----------- ----------------------------------------- 1 1 part_id is 1 category_id 11 2 part_id is 1 category_id 21 3 part_id is 1 category_id 32 1 part_id is 2 category_id 12 2 part_id is 2 category_id 22 3 part_id is 2 category_id 3
as you can see, by using a nested while loop each combination of part_id and category_id is unique. the code within the first while loop controlled the incrementing of the part_id, where as the second while loop set the category_id to a different value each time through the loop. the code within the first while loop was executed only twice, but the code inside the second while loop was executed 6 times. thus giving me 6 sample part records.
break and continue statements
now sometimes you want to build a loop that will process through logically to the end most of the time, but not all the time. in other words, you may want to break out of the loop if some particular condition arises. also in addition to breaking out of the loop, you may not want to process all the code in the loop before going back to the top of the loop and starting through the next iteration of the loop. for these kinds of programming requirements sql server provides the break and continue statements.
the break statement exits out of the inner most while loop, and proceeds to the statement following the end statement that is associated with the loop in which the break statement is executed. the continue statement skips executing the rest of the statements between the continue statement and the end statement of the current loop and starts executing at the first line following the begin statement of the current while loop. let's go though a couple of break and continue examples.
for the break statement i'm going to modify my last example that generated part table records. this time i'm going to break out of the inner while loop when category_id is 2 and part_id is 1. here is my code for the break statement.
declare @part_id intdeclare @category_id intdeclare @desc varchar(50)create table part (part_id int, category_id int, description varchar(50))set @part_id = 0set @category_id = 0 while @part_id < 2begin set @part_id = @part_id + 1 while @category_id < 3 begin set @category_id = @category_id + 1 if @category_id = 2 and @part_id = 1 break set @desc = 'part_id is ' + cast(@part_id as char(1)) + ' category_id ' + cast(@category_id as char(1)) insert into part values(@part_id, @category_id, @desc ) end set @category_id = 0 endselect * from partdrop table part
here is the output for this code that contains a break statement inside the inner while loop.
part_id category_id description ----------- ----------- ----------------------------------------- 1 1 part_id is 1 category_id 12 1 part_id is 2 category_id 12 2 part_id is 2 category_id 22 3 part_id is 2 category_id 3
from this output you can see that no records were inserted for part_id = 1 and category_id =2 or 3, where as there are records for part_id = 2 with all values for the category_id column. this is because the if statement in the inner loop forced the break statement to exit the inner loop. since there were records generate for part_id = 2, shows that the break statement only exited the inner loop and not the outer loop.
now just to stay with the same example i've been using, let's replace the break statement in the code above with a continue statement. here is the code for demonstrating the continue statement.
declare @part_id intdeclare @category_id intdeclare @desc varchar(50)create table part (part_id int, category_id int, description varchar(50))set @part_id = 0set @category_id = 0 while @part_id < 2begin set @part_id = @part_id + 1 while @category_id < 3 begin set @category_id = @category_id + 1 if @category_id = 2 and @part_id = 1 continue set @desc = 'part_id is ' + cast(@part_id as char(1)) + ' category_id ' + cast(@category_id as char(1)) insert into part values(@part_id, @category_id, @desc ) end set @category_id = 0 endselect * from partdrop table part
when you use the continue statement you get the following output.
----------- ----------- ----------------------------------------- 1 1 part_id is 1 category_id 11 3 part_id is 1 category_id 32 1 part_id is 2 category_id 12 2 part_id is 2 category_id 22 3 part_id is 2 category_id 3
as you can see, when i use the continue statement only the record with category_id = 2 and part_id = 1 is missing. this is because the continue statement does not break out of the inner while loop but only goes back to the top of the while loop without inserting the record. this happens only when category_id is 2 and part_id is equal to 1. when part_id = 1 and category_id = 3 the insert statement is still executed.
goto statement
the break statement will only exit you from the currently processing while loop, it will not break out of all while loops. however, occasionally this is the kind of functionality your t-sql script needs. to have your code break out of all while loops, no matter how many nested while statements you have, you will need to use the goto statement. now i know most programmers cringe at the thought of using the goto statement, but in this case i feel the goto is an except able practice. using my same example i will use the goto to break out of both while loops, when the part_id = 1 and the category_id=3.
declare @part_id intdeclare @category_id intdeclare @desc varchar(50)create table part (part_id int, category_id int, description varchar(50))set @part_id = 0set @category_id = 0 while @part_id < 2begin set @part_id = @part_id + 1 while @category_id < 3 begin set @category_id = @category_id + 1 if @category_id = 3 and @part_id = 1 goto break_out set @desc = 'part_id is ' + cast(@part_id as char(1)) + ' category_id ' + cast(@category_id as char(1)) insert into part values(@part_id, @category_id, @desc ) end set @category_id = 0 endbreak_out:select * from partdrop table part
here is the output from this goto code:
part_id category_id description ----------- ----------- ----------------------------------------- 1 1 part_id is 1 category_id 11 2 part_id is 1 category_id 2
here the goto logic stopped the insertion of records into the part table when @category_id = 3 and @part_id = 1. this is done by executing the "goto breakout" statement. note that when this goto statement was executed it branched to the label "break out:" which can be found following the end statement for the first, outer most while statement.
conclusion
hopefully now you have a better idea of how to code a t-sql while loop. i've explained how to control the while loop, break out of a loop by using the break statement, use the continue statement to skip some of the code in the while loop, and/or break out of all while loops using the goto statement. the techniques i've described should give you the basis for building all your while statements from a single while loop to a complex set of nested while loops. my next article in this series will discuss how to process through a set of records.