Parsing comma separated values into variables. (merged 6) [message #441752] |
Wed, 03 February 2010 00:30 |
anil029
Messages: 15 Registered: February 2010
|
Junior Member |
|
|
Hi,
I am trying to parse a string which contains comma separated values.The string contains more than 100 values.I want to capture the values in PL/SQL variables so that I can insert the values in the columns of database table.I know it can be done by using SUBSTR and INSTR functions separately for each values.Can it be done in a loop to avoid writing the same piece of code again and again for each values.
Example:
Suppose the input string is as string :='val1,val2,val3,............................'.
The values needs to be stored in separate variables as
var1:=val1
var2:=val2
...........
...........
Please help to resolve this issue and let me know if further clarification is needed.
Thanks,
Anil
|
|
|
|
|
Re: Parsing comma separated values into variables. [message #441763 is a reply to message #441757] |
Wed, 03 February 2010 00:52 |
anil029
Messages: 15 Registered: February 2010
|
Junior Member |
|
|
Hi Michael,
The solution provided in the url is just displaying the values in the string.lastly,when the loop ends 's' contains last value in the string.I want to store each values in some predefined pl/sql variables so that i can insert them in a database table.
Thanks,
anil
|
|
|
|
Re: Parsing comma separated values into variables. [message #441765 is a reply to message #441763] |
Wed, 03 February 2010 00:56 |
|
Michel Cadot
Messages: 68728 Registered: March 2007 Location: Saint-Maur, France, https...
|
Senior Member Account Moderator |
|
|
anil029 wrote on Wed, 03 February 2010 07:52Hi Michael,
The solution provided in the url is just displaying the values in the string.lastly,when the loop ends 's' contains last value in the string.I want to store each values in some predefined pl/sql variables so that i can insert them in a database table.
Thanks,
anil
I provided an example of a way to do it with INSTR and SUBSTR not a direct solution to YOUR issue, it is easy to adapt it to your case, just analyze how it works and you will find how to do it in PL/SQL.
The core is:
substr(:myString,
instr('|'||:myString||'|', '|', 1, rn),
instr('|'||:myString||'|', '|', 1, rn+1)
- instr('|'||:myString||'|', '|', 1, rn)
where rn indicates the number of your variable: 1 for var1, 2 for var 2 and so on (and replacing | by , for your case).
Regards
Michel
[Updated on: Wed, 03 February 2010 00:59] Report message to a moderator
|
|
|
Re: Parsing comma separated values into variables. [message #441767 is a reply to message #441764] |
Wed, 03 February 2010 01:04 |
mohley_s
Messages: 10 Registered: January 2010 Location: bangalore, india
|
Junior Member |
|
|
DECLARE
CURSOR EMP_CUR IS SELECT A.EMPNO,A.ENAME,B.ENAME AS MANAGER, A.SAL
FROM EMP A JOIN EMP B ON (A.EMPNO = B.EMPNO)
WHERE A.DEPTNO = 30;
TYPE EMP_TAB_TYPE IS TABLE OF EMP_CUR%ROWTYPE;
EMP_TAB EMP_TAB_TYPE;
BEGIN
EMP_TAB := EMP_TAB_TYPE();
OPEN EMP_CUR;
FETCH EMP_CUR BULK COLLECT INTO EMP_TAB;
CLOSE EMP_CUR;
IF EMP_TAB.FIRST IS NOT NULL THEN
FOR I IN EMP_TAB.FIRST .. EMP_TAB.LAST
LOOP
dbms_output.put_line(EMP_TAB(I).EMPNO ||' '||EMP_TAB(I).ENAME||' '||
EMP_TAB(I).MANAGER||' '||EMP_TAB(I).SAL);
END LOOP;
END IF;
END;
hope it will be of some help
regards,
mohley
[EDITED by LF: applied [code] tags]
[Updated on: Wed, 03 February 2010 01:34] by Moderator Report message to a moderator
|
|
|
|
|
|
|