Oracle PL/SQL - Function Returns Associative Array Indexed by PLS_INTEGER

Introduction

The following code defines a type of associative array indexed by PLS_INTEGER and a function that returns an associative array of that type.

Demo

SQL>
SQL> DECLARE-- from ww w . j a v a 2 s  . co  m
  2    TYPE sum_multiples IS TABLE OF PLS_INTEGER INDEX BY PLS_INTEGER;
  3    n  PLS_INTEGER := 6;  
  4    sn PLS_INTEGER := 20; 
  5    m  PLS_INTEGER := 2;  
  6
  7    FUNCTION get_sum_multiples(multiple IN PLS_INTEGER,num IN PLS_INTEGER)RETURN sum_multiples
  8    IS
  9      s sum_multiples;
 10    BEGIN
 11      FOR i IN 1..num LOOP
 12        s(i) := multiple * ((i * (i + 1)) / 2); 
 13      END LOOP;
 14      RETURN s;
 15    END get_sum_multiples;
 16
 17  BEGIN
 18      DBMS_OUTPUT.PUT_LINE ('Sum of the first ' || TO_CHAR(n) || ' multiples of ' ||
 19        TO_CHAR(m) || ' is ' || TO_CHAR(get_sum_multiples (m, sn)(n))
 20      );
 21  END;
 22  /
Sum of the first 6 multiples of 2 is 42

PL/SQL procedure successfully completed.

SQL>

Related Topic