Thursday, July 2, 2009

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.

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. In many situations, the number of times a loop must execute varies, so the FOR loop cannot be used each time.

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;


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;


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;

No comments:

SpringBoot: Features: SpringApplication

Below are a few SpringBoot features corresponding to SpringApplication StartUp Logging ·          To add additional logging during startup...