Question

Can anyone help me how can I get the value of array inside an array in for loop ORACLE. This is a example IN PARAMETER Array. I want to get the value of array is the array [3] in "ID" and VALUE of ID "EMP01658".

Array ( [1] => DC0001 [2] => DC0002 [3] => Array ( [ID] => EMP01658 ) )

This is my PL SQL CODE;

--This is my Package global variables

CREATE OR REPLACE PACKAGE PKG_GLB_VARIABLES 
IS 

  TYPE refcursor        IS REF CURSOR;                        
  TYPE val_Array        IS TABLE OF VARCHAR2(3000) INDEX BY BINARY_INTEGER;
END;

--This is my Procedure

CREATE OR REPLACE PROCEDURE Proc_Array(
    param_Array     IN pkg_glb_variables.val_Array
)
IS

BEGIN
    for i in 1 .. param_Array.count
      loop
        DBMS_OUTPUT.put_line('aRRay('||i||') :'||param_Array(i));

        IF param_Array(i)='Array' THEN
        DBMS_OUTPUT.put_line('UY MAY ISA PANG ARRAY!');
           for i_2 in 1 .. param_Array(i).count
             loop
                DBMS_OUTPUT.put_line('aRRay('||i_2||') :'||param_Array(i_2));
             end loop;     
        END IF;
      end loop;  


END;

Thank you. :)

Was it helpful?

Solution

A VARCHAR2 is not the same as an array (collection or PL/SQL table), and cannot be indexed as such. You need to use Oracle's String functions. Here is an example to print out each character of each varchar2 array element:

CREATE OR REPLACE PROCEDURE Proc_Array(
    param_Array     IN pkg_glb_variables.val_Array
)
IS
vElement VARCHAR2(3000);
vLength  PLS_INTEGER;

BEGIN
    for i in 1 .. param_Array.count
      loop
        DBMS_OUTPUT.put_line('aRRay('||i||') :'||param_Array(i));

        vElement := param_array(i);
        vLength  := length(vElement);

           for i_2 in 1 .. vLength
             loop
                DBMS_OUTPUT.put_line('aRRay('||i_2||') :'||
                           substr(vElement,i_2,1) );
             end loop;     
      end loop;  
END;

A test to call the procedure:

declare
a   pkg_glb_variables.val_Array;
begin
  a(1) := 'HELLO';
  a(2) := 'goodbye';
  proc_array ( a );
end;

The resulting dbms_output:

my output - aRRay(1) :HELLO
my output - aRRay(1) :H
my output - aRRay(2) :E
my output - aRRay(3) :L
my output - aRRay(4) :L
my output - aRRay(5) :O
my output - aRRay(2) :goodbye
my output - aRRay(1) :g
my output - aRRay(2) :o
my output - aRRay(3) :o
my output - aRRay(4) :d
my output - aRRay(5) :b
my output - aRRay(6) :y
my output - aRRay(7) :e
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top