1

I want to build an array based on a comma separated string . But cant hwlp myslef any further .


DECLARE
type rol_type is record 
(role  MMSTROLEHDR.ROLECODE%TYPE) ;

array_rolecode rol_type; vl_prmval VARCHAR2 (4000) := '2,3,4'; vl_pos NUMBER; BEGIN WHILE (INSTR (vl_prmval, ',') > 0) LOOP vl_pos := INSTR (vl_prmval, ',');

  --vl_cnt := vl_cnt + 1;
  array_rolecode.role := SUBSTR (vl_prmval, 1, vl_pos - 1);
  vl_prmval := SUBSTR (vl_prmval, vl_pos + 1);

END LOOP;

FOR j IN array_rolecode.first .. array_rolecode.last LOOP DBMS_OUTPUT.put_line (array_rolecode.role); END LOOP; END; END;

BenMorel
  • 34,448
  • 50
  • 182
  • 322
HalfWebDev
  • 7,022
  • 12
  • 65
  • 103
  • 4
    Duplicate of [Convert comma separated string to array in PL/SQL](http://stackoverflow.com/questions/3819375/convert-comma-separated-string-to-array-in-pl-sql) – Yann39 Oct 04 '13 at 06:56

1 Answers1

0

Your array must be declared as array, not as a record. It seems, there is an error in cycle, cause the last part of the string not being inserted in array, you should check it. However, here is the working script:

DECLARE
type rol_type is record 
(role  MMSTROLEHDR.ROLECODE%TYPE) ;

type rol_arr is table of rol_type
index by binary_integer;

--array_rolecode  rol_type;
array_rolecode rol_arr;
   vl_prmval        VARCHAR2 (4000) := '2,3,4';
   vl_pos           NUMBER := 0;
BEGIN
   WHILE (INSTR (vl_prmval, ',') > 0)
   LOOP
      vl_pos := INSTR (vl_prmval, ',');

  --vl_cnt := vl_cnt + 1;
  array_rolecode(NVL(array_rolecode.last+1,1)).role := SUBSTR (vl_prmval, 1, vl_pos - 1);
  vl_prmval := SUBSTR (vl_prmval, vl_pos + 1);


END LOOP;

FOR j IN array_rolecode.first .. array_rolecode.last
   LOOP
      DBMS_OUTPUT.put_line (array_rolecode(j).role);
END LOOP;
END;
Mikhail
  • 1,540
  • 2
  • 13
  • 13