Home » SQL & PL/SQL » SQL & PL/SQL » Please help - comma separated values (Oracle Database 11g Release 11.2.0.3.0)
Please help - comma separated values [message #610943] Wed, 26 March 2014 04:10 Go to next message
oralover2006
Messages: 144
Registered: January 2010
Location: India
Senior Member
hi all gurus,

I tried to generate a routine which get a list of parameters in comma separated single variable value, below is m_msg for example and want to get all values one by one for that routine. it works when all values are in same length as shown in 1st PL/SQL example but when any one value's length is change like in 2nd example, the statement showing wrong results. where I made mistake? what I missed?

Please help.

  1* select banner from v$version
SQL> /

BANNER
--------------------------------------------------------------------------------
Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
PL/SQL Release 11.2.0.3.0 - Production
CORE    11.2.0.3.0      Production
TNS for 64-bit Windows: Version 11.2.0.3.0 - Production
NLSRTL Version 11.2.0.3.0 - Production

5 rows selected.

SQL> ed
Wrote file afiedt.buf

  1  DECLARE
  2     m_msg  VARCHAR2(100) := 'VALUEXX1,VALUEAA2,VALUEBB3,';
  3     v_msg  VARCHAR2(100);
  4     x_len  NUMBER := length(m_msg);
  5     x1     NUMBER := 1;
  6  BEGIN
  7     while x1 <= length(m_msg)
  8     LOOP
  9        Select substr(m_msg,x1,instr(m_msg,',')-1) into v_msg from dual;
 10        x1 := x1+length(v_msg)+1;
 11        dbms_output.put_line(x1||' - '||v_msg);
 12     END LOOP;
 13* END;
SQL> /
10 - VALUEXX1
19 - VALUEAA2
28 - VALUEBB3

PL/SQL procedure successfully completed.

SQL> ed
Wrote file afiedt.buf

  1  DECLARE
  2     m_msg  VARCHAR2(100) := 'VALUEX1,VALUEAA2,VALUEBB3,';  -- here I change from VALUEXX1 -> VALUEX1 
  3     v_msg  VARCHAR2(100);
  4     x_len  NUMBER := length(m_msg);
  5     x1     NUMBER := 1;
  6  BEGIN
  7     while x1 <= length(m_msg)
  8     LOOP
  9        Select substr(m_msg,x1,instr(m_msg,',')-1) into v_msg from dual;
 10        x1 := x1+length(v_msg)+1;
 11        dbms_output.put_line(x1||' - '||v_msg);
 12     END LOOP;
 13* END;
SQL> /
9 - VALUEX1
17 - VALUEAA
25 - ,VALUEB
28 - 3,

PL/SQL procedure successfully completed.

SQL> 


regards.

[Updated on: Wed, 26 March 2014 04:30]

Report message to a moderator

Re: Please help - comma separated values [message #610945 is a reply to message #610943] Wed, 26 March 2014 04:34 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

SQL> var mylist varchar2(100)
SQL> exec :mylist := '5,11,13,22,23,31,44,45'

PL/SQL procedure successfully completed.

SQL> select substr(:mylist,
  2                instr(','||:mylist||',', ',', 1, rn),
  3                instr(','||:mylist||',', ',', 1, rn+1)
  4                - instr(','||:mylist||',', ',', 1, rn) - 1) value
  5  from ( select level rn from dual 
  6         connect by level 
  7                      <= length(:mylist)-length(replace(:mylist,',',''))+1
  8       )
  9  /
VALUE
-----------------------------------------------------------
5
11
13
22
23
31
44
45

[Updated on: Wed, 26 March 2014 04:34]

Report message to a moderator

Re: Please help - comma separated values [message #610946 is a reply to message #610945] Wed, 26 March 2014 05:00 Go to previous messageGo to next message
oralover2006
Messages: 144
Registered: January 2010
Location: India
Senior Member
thanks for quick response, always you are helping me Smile

please inform, can I use given SQL statement in my PL/SQL block to have one value at a time to process (to call another function / procedure)? it is generating all data in a go.

regards.
Re: Please help - comma separated values [message #610947 is a reply to message #610946] Wed, 26 March 2014 05:04 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
You can use bulk collect to fetch all the values into an array or use a for loop.
Re: Please help - comma separated values [message #610950 is a reply to message #610947] Wed, 26 March 2014 05:15 Go to previous messageGo to next message
oralover2006
Messages: 144
Registered: January 2010
Location: India
Senior Member
oh yes!
thanks cookiemonster for advise Smile

SQL> declare
  2    type string_array is varray(100) of varchar2(100);
  3    a_mylist string_array;
  4    mylist   VARCHAR2(100) := 'VALUE_X1,VALUE_XX2,VALUE_XXX3';
  5    --
  6    cursor c1 is
  7           select substr(mylist,
  8                instr(','||mylist||',', ',', 1, rn),
  9                instr(','||mylist||',', ',', 1, rn+1)
 10                - instr(','||mylist||',', ',', 1, rn) - 1) value
 11            from ( select level rn from dual
 12                 connect by level 
 13                       <= length(mylist)-length(replace(mylist,',',''))+1
 14                 );
 15  begin
 16    open c1;
 17    fetch c1 bulk collect into a_mylist;
 18    close c1;
 19    for indx in a_mylist.first..a_mylist.last loop
 20        dbms_output.put_line ( a_mylist(indx) );
 21    end loop;
 22  end;
 23  .
SQL> /
VALUE_X1
VALUE_XX2
VALUE_XXX3

PL/SQL procedure successfully completed.



regards.

[Updated on: Wed, 26 March 2014 05:23]

Report message to a moderator

Re: Please help - comma separated values [message #610958 is a reply to message #610950] Wed, 26 March 2014 05:46 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Thanks for your feedback.

Re: Please help - comma separated values [message #610961 is a reply to message #610958] Wed, 26 March 2014 05:50 Go to previous message
oralover2006
Messages: 144
Registered: January 2010
Location: India
Senior Member
and many thanks for your help Smile

and cookiemonster you too...
Previous Topic: Oracle transpose rows into columns
Next Topic: where can i call the procedure in the procedure in the package
Goto Forum:
  


Current Time: Fri Apr 26 05:11:17 CDT 2024