Solved

return an array of data from a pl/sql function

Posted on 2009-04-01
3
1,773 Views
Last Modified: 2013-12-07
For example in the function
Enter
Select task_status
      From task_table
Where task_id =  passed_id

Load the results of the selects into an array
Return the array back to the module that called the function

Within  the module that called the function, how do I iterate through the array to get the values? How to I determine the number of times to loop through the array with ?

Could you please give me an example?
0
Comment
Question by:cookiejar
3 Comments
 
LVL 11

Accepted Solution

by:
yuching earned 250 total points
ID: 24045763
Perhaps this help

DECLARE
  TYPE tArray IS TABLE Of Varchar2(100) INDEX BY Binary_Integer;
  tStatusArray tArray;
  l Integer;
FUNCTION get_array RETURN tArray IS
  t tArray;
  i  INTEGER := 1;  
  BEGIN
      FOR r In (Select task_status From task_table Where task_id =  passed_id)
      Loop
        t(i) := r.task_status;
        i := i +1;
      END LOOP;
    RETURN t;
  END get_array;
 
BEGIN
-- interate the array from function
   tStatusArray := get_array;
   For l In tStatusArray.First .. tStatusArray.Last
   Loop
          dbms_output.put_line(tStatusArray(l));
   End Loop;
END;
0
 
LVL 73

Assisted Solution

by:sdstuber
sdstuber earned 250 total points
ID: 24045847
Here's a simple example showing how to build an associative array with a query, return that array from a function.

call the function from a procedure which then iterates through the generated array printing each element
CREATE OR REPLACE PACKAGE test_pkg
IS
    TYPE array_of_tables
    IS
        TABLE OF all_tables.table_name%TYPE
            INDEX BY BINARY_INTEGER;
 
 
    FUNCTION get_tables(p_owner IN VARCHAR2)
        RETURN array_of_tables;
 
    PROCEDURE print_tables(p_owner IN VARCHAR2);
END;
 
 
 
CREATE OR REPLACE PACKAGE BODY test_pkg
IS
    FUNCTION get_tables(p_owner IN VARCHAR2)
        RETURN array_of_tables
    IS
        v_array   array_of_tables;
    BEGIN
        SELECT   table_name
          BULK   COLLECT
          INTO   v_array
          FROM   all_tables
         WHERE   owner = p_owner;
 
        RETURN v_array;
    END get_tables;
 
    PROCEDURE print_tables(p_owner IN VARCHAR2)
    IS
        v_array   array_of_tables;
    BEGIN
        v_array   := get_tables(p_owner);
 
        FOR i IN 1 .. v_array.COUNT
        LOOP
            DBMS_OUTPUT.put_line(v_array(i));
        END LOOP;
    END;
END;
 
begin
test_pkg.print_tables('SOME_USER');
end;

Open in new window

0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Export table into csv file in oracle 10 97
1 FROM DUAL wont work with additional columns ?? 4 37
Problem with duplicate records in Oracle query 16 31
create a nested synonym 4 26
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question