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.
... 下一页