Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Function returning collection - Rows to columns

partlycloudyApr 2 2019 — edited Apr 3 2019

Oracle 12.1

How can I integrate this function into a SQL query (ID is one of the columns in the query) to get the 3 rows returned by the function as 3 columns in the query for each applicable ID?

Sample code below

Any ideas appreciated. Thanks.

CREATE OR REPLACE FUNCTION get_details (

        p_id IN INTEGER

    )

    RETURN apex_t_varchar2

is

  l_output apex_t_varchar2 := apex_t_varchar2();

begin

   l_output.extend(3);

   if (p_id = 1)

   then

     l_output(1) := 'Apple';

     l_output(2) := 'Fruit';

     l_output(3) := 'Citrus';

   elsif (p_id = 2)

   then

       l_output(1) := 'Tiger';

       l_output(2) := 'Animal';

       l_output(3) := 'Wild';

   end if;          

   return l_output;         

end get_details;

select * from table(get_details(1))

select * from table(get_details(2))

select

  mod(level,2)+1 id

from dual d

connect by level <= 10

This post has been answered by Solomon Yakobson on Apr 3 2019
Jump to Answer
Comments
Post Details
Added on Apr 2 2019
10 comments
3,886 views