Transact-SQL Operators: WHILE |
|
Introduction |
To examine a condition and evaluate it before taking action, you can use the WHILE operator. The basic formula of this statement is: WHILE Expression Statement When implementing this statement, first provide an Expression after the WHILE keyword. The Expression must produce a true or a false result. If the Expression is true, then the interpreter executes the Statement. After executing the Statement, the Expression is checked again. AS LONG AS the Expression is true, it will keep executing the Statement. When or once the Expression becomes false, it stops executing the Statement. |
This scenario can be illustrated as follows: Here is an example: DECLARE @Number As int WHILE @Number < 5 SELECT @Number AS Number GO To effectively execute a while condition, you should make sure you provide a mechanism for the interpreter to get a referenced value for the condition, variable, or expression being checked. This is sometimes in the form of a variable being initialized although it could be some other expression. Such a while condition could be illustrated as follows:
This time, the statement would be implemented as follows: DECLARE @Number As int SET @Number = 1 WHILE @Number < 5 BEGIN SELECT @Number AS Number SET @Number = @Number + 1 END GO This would produce: |
|
||
Home | Copyright © 2007-2008 FunctionX, Inc. | |
|