Section 1.5. Loops in PLSQL


1.5. Loops in PL/SQL

PL/SQL offers three types of loops to provide you with the flexibility you need to write the most straightforward code to handle any particular situation. Most situations that require a loop could be written with any of the three loop constructs. If you do not pick the construct that is best suited for that particular requirement, however, you could end up having to write many additional lines of code. The resulting module would also be harder to understand and maintain.

To give you a feeling for the way the different loops solve their problems in different ways, consider the following three procedures. In each case, the procedure makes a call to display_total_sales for a particular year, for each year number between the start and end argument values.

In the following examples, the FOR loop clearly requires the smallest amount of code. Yet I can use it in this case only because I know that I will run the body of the loop a specific number of times. In many other situations, the number of times a loop must execute varies, so the FOR loop cannot be used.

1.5.1. Simple Loop

The simple loop is called simple for a reason: it starts simply with the LOOP keyword and ends with the END LOOP statement . The loop will terminate if you execute an EXIT, EXIT WHEN, or RETURN within the body of the loop (or if an exception is raised):

     PROCEDURE display_multiple_years (        start_year_in IN PLS_INTEGER       ,end_year_in IN PLS_INTEGER     )     IS        l_current_year PLS_INTEGER := start_year_in;     BEGIN        LOOP           EXIT WHEN l_current_year > end_year_in;           display_total_sales (l_current_year);           l_current_year :=  l_current_year + 1;        END LOOP;     END display_multiple_years;

1.5.2. FOR Loop

Oracle offers both numeric and cursor FOR loops . With the numeric FOR loop, you specify the start and end integer values, and PL/SQL does the rest of the work for you, iterating through each intermediate value, and then terminating the loop:

     PROCEDURE display_multiple_years (        start_year_in IN PLS_INTEGER       ,end_year_in IN PLS_INTEGER     )     IS     BEGIN        FOR l_current_year IN start_year_in .. end_year_in        LOOP           display_total_sales (l_current_year);        END LOOP;     END display_multiple_years;

The cursor FOR loop has the same basic structure, but, in this case, you supply an explicit cursor or SELECT statement in place of the low-high integer range:

     PROCEDURE display_multiple_years (        start_year_in IN PLS_INTEGER       ,end_year_in IN PLS_INTEGER     )     IS     BEGIN        FOR l_current_year IN (           SELECT * FROM sales_data            WHERE year BETWEEN start_year_in AND end_year_in)        LOOP           -- This procedure is now accepted a record implicitly declared           -- to be of type sales_data%ROWTYPE...           display_total_sales (l_current_year);        END LOOP;     END display_multiple_years;

1.5.3. WHILE Loop

The WHILE loop is very similar to a simple loop, with a critical difference being that it checks the termination condition up front. It may not even execute its body a single time:

     PROCEDURE display_multiple_years (        start_year_in IN PLS_INTEGER       ,end_year_in IN PLS_INTEGER     )     IS        l_current_year PLS_INTEGER := start_year_in;     BEGIN        WHILE (l_current_year <= end_year_in)        LOOP           display_total_sales (l_current_year);           l_current_year :=  l_current_year + 1;        END LOOP;     END display_multiple_years;




Oracle PL(s)SQL For DBAs
Oracle PL(s)SQL For DBAs
ISBN: N/A
EAN: N/A
Year: 2005
Pages: 122

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net