Tham khảo tài liệu 'pl/sql user's guide and reference 10g release phần 3', công nghệ thông tin, cơ sở dữ liệu phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả | Controlling Loop Iterations LOOP and EXIT Statements END LOOP A statement inside the loop must assign a new value to the Boolean variable to avoid an infinite loop. Using the FOR-LOOP Statement Simple FOR loops iterate over a specified range of integers. The number of iterations is known before the loop is entered. A double dot . serves as the range operator FOR counter IN REVERSE LOOP sequence_of_statements END LOOP The range is evaluated when the FOR loop is first entered and is never re-evaluated. As the next example shows the sequence of statements is executed once for each integer in the range. After each iteration the loop counter is incremented. FOR i IN LOOP -- assign the values 1 2 3 to i sequence_of_statements -- executes three times END LOOP If the lower bound equals the higher bound the loop body is executed once FOR i IN LOOP -- assign the value 3 to i sequence_of_statements -- executes one time END LOOP By default iteration proceeds upward from the lower bound to the higher bound. If you use the keyword REVERSE iteration proceeds downward from the higher bound to the lower bound. After each iteration the loop counter is decremented. You still write the range bounds in ascending not descending order. FOR i IN REVERSE LOOP -- assign the values 3 2 1 to i sequence_of_statements -- executes three times END LOOP Inside a FOR loop the counter can be read but cannot be changed FOR ctr IN LOOP IF NOT finished THEN INSERT INTO . VALUES ctr . -- OK factor ctr 2 -- OK ELSE ctr 10 -- not allowed END IF END LOOP Tip A useful variation of the FOR loop uses a SQL query instead of a range of integers. This technique lets you run a query and process all the rows of the result set with straightforward syntax. For details see Querying Data with PL SQL Implicit Cursor FOR Loop on page 6-9. How PL SQL Loops Iterate The bounds of a loop range can be literals variables or expressions but must evaluate to numbers. Otherwise PL SQL .