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!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

return multiple rows from a function

Sreenivas NJul 29 2014 — edited Jul 30 2014

Hi Gurus,

I have to create a function which take list of values(ex: 234,235,245,123) as input returns multiple row output.

create or replace

FUNCTION concatenate_list (p_in VARCHAR2)

  RETURN  VARCHAR2

IS

  l_return  VARCHAR2(32767);

BEGIN

    SELECT ven_mod || ': '|| names into l_return from tobject where object_id in (p_in); 

  RETURN LTRIM(l_return);

END;

I have to show all of them in single row. when I use listagg function it says the limit exceeds 4000 chars (if the list of input parameter is huge) and I can't use wm_concat as it is not oracle documented.

can you please suggest how can I return all those values in single go.

thank you.

Comments

Chanchal Wankhade

Hi,

Ideally you should not try to get the multiple row result as how you are trying to.

You cannot get the desired result as per the way you are trying to get it.

we cannot provide parameter values like you have providing. for that you need to if you are trying to get the resule for  total 4 object id then you need to declare 4 parameter in function and you need to use IN clauase in where condition like shown below:-

create or replace

FUNCTION concatenate_list (p1_in VARCHAR2,p2_in VARCHAR2,p3_in VARCHAR2,p4_in VARCHAR2)

  RETURN  VARCHAR2

IS

  l_return  VARCHAR2(32767);

BEGIN

    SELECT ven_mod || ': '|| names into l_return from tobject where object_id in (p1_in,p2_in,p3_in,p4_in);

  RETURN LTRIM(l_return);

END;

Regards,
Chanchal Wankhade.

994122

Hi,

    Please try this..

CREATE OR REPLACE FUNCTION APPS.concatenate_list (p_cursor IN  SYS_REFCURSOR)

  RETURN  VARCHAR2

IS

  l_return  VARCHAR2(32767);

  l_temp    VARCHAR2(32767);

BEGIN

  LOOP

    FETCH p_cursor

    INTO  l_temp;

    EXIT WHEN p_cursor%NOTFOUND;

    l_return := l_return || ',' || l_temp;

  END LOOP;

  RETURN LTRIM(l_return, ',');

END;

source from

ORACLE-BASE - String Aggregation Techniques

961315

select substr(str,instr(str,',',1,level)+1,instr(str,',',1,level+1)-instr(str,',',1,level)-1) from (select ','||'11,12,13,14'||',' str from dual )

connect by level<= length(str)-length(replace(str,',',''))-1

Sreenivas N

thanks for reply.

I have to pass the object_id values dynamically, I dont know the list of values passing to this function (might be 1 or 10 or 100).

Sreenivas N

suppose I have select statement like

SELECT LISTAGG(level, ', ') WITHIN GROUP( ORDER BY level)  FROM dual CONNECT BY LEVEL <= 200;

its returning results, but when I change the value to 2000 then its giving me the error. (ORA-01489: result of string concatenation is too long)

Is there any other way to handle this. (I dont want to use wm_concat, as it is not oracle documented)

user4585215


Hi,

Following workaround may meet your requirement:

create or replace FUNCTION concatenate_list (p_in VARCHAR2)

  RETURN  clob

IS

  l_return  VARCHAR2(32767);

  v_query varchar2(4000);

  temp_str varchar2(1000);

 

  cur_strlist sys_refcursor;

 

BEGIN

  v_query := 'SELECT  ven_mod || '': '' || names  as concat_str from tobject  where  object_id  in (' ||

              p_in || ')';

 

  open cur_strlist for v_query;

  loop

    fetch cur_strlist into temp_str;

    exit when cur_strlist%notfound;

   

    l_return := l_return || '~' || temp_str;

  end loop;

 

  close cur_strlist;

 

  RETURN substr(l_return,2);

END;

AnnEdmund

Try this...

SELECT XMLAGG(XMLELEMENT(e,DECODE(LEVEL,1,'',',')||LEVEL).EXTRACT('//text()')) FROM dual CONNECT BY LEVEL <= 2000;

1 - 7
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Aug 27 2014
Added on Jul 29 2014
7 comments
8,013 views