Home » SQL & PL/SQL » SQL & PL/SQL » Comma separated (Oracle 11g)
Comma separated [message #612806] Fri, 25 April 2014 00:58 Go to next message
srinivas.k2005
Messages: 404
Registered: August 2006
Senior Member
Hi,

Requirement:
I need to get the each value from a string (comma separated) and pass the each value as parameter to the procedure inside the for loop.


Example requirement like below:

DECLARE
  v_a  VARCHAR2(100):= '1,2,3,4,5,6';
BEGIN

For i in (v_a )  loop

Get_Procedure_Status (Each value from variable v_a - comma separated);-- Procedure

End loop;

END;
/



Please advise.

Regards,
SRK


Re: Comma separated [message #612808 is a reply to message #612806] Fri, 25 April 2014 01:05 Go to previous messageGo to next message
Littlefoot
Messages: 21808
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
Here's one option:
SQL> declare
  2    v_a varchar2(30) := '1,2,3,4,5,6';
  3  begin
  4    for cur_r in (select regexp_substr (v_a, '[^,]+', 1, level) result
  5                  from dual
  6                  connect by regexp_substr (v_a, '[^,]+', 1, level) is not null
  7                 )
  8    loop
  9      dbms_output.put_line(cur_r.result);
 10    end loop;
 11  end;
 12  /
1
2
3
4
5
6

PL/SQL procedure successfully completed.

SQL>
Re: Comma separated [message #612809 is a reply to message #612806] Fri, 25 April 2014 01:07 Go to previous messageGo to next message
Michel Cadot
Messages: 68647
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

This is a FAQ.
Here's a starter:
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

Re: Comma separated [message #612810 is a reply to message #612809] Fri, 25 April 2014 01:16 Go to previous messageGo to next message
srinivas.k2005
Messages: 404
Registered: August 2006
Senior Member
Thank you so Much
Re: Comma separated [message #612834 is a reply to message #612810] Fri, 25 April 2014 09:45 Go to previous messageGo to next message
anil_mk
Messages: 146
Registered: August 2006
Location: Bangalore, India
Senior Member

Hi Reddy,

You can below code as well

DECLARE 
nt_email_catalogid sys.ODCINumberList := sys.ODCINumberList();
BEGIN 


 FOR i IN 1..5 
  LOOP
      nt_email_catalogid.extend;
      nt_email_catalogid(i):=10*i;
 END LOOP;
 
 FOR i IN (SELECT COLUMN_VALUE FROM TABLE(nt_email_catalogid))
          LOOP
        DBMS_OUTPUT.PUT_LINE(I.COLUMN_VALUE);
END LOOP;
END;


Thanks,
Anil MK
Re: Comma separated [message #612835 is a reply to message #612834] Fri, 25 April 2014 09:53 Go to previous message
Michel Cadot
Messages: 68647
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Note that the input is NOT an array, it is a string.
This is a totally different question.

Previous Topic: Find out last four digits
Next Topic: Nested table
Goto Forum:
  


Current Time: Fri Apr 26 19:13:32 CDT 2024