18

Can you please help me how can I create two dimensional array in PL/SQL for Stored Procedure? The columns are dynamic so it can grow and change in types also. Any help is appreciated. Thank you in advance!

I have the following code:

Type CAR_TABLE_ARRAY is varray(2) of varchar2(255);
TYPE CAR_TABLE_TYPE IS TABLE OF CAR_TABLE_ARRAY;
CAR_TABLE CAR_TABLE_TYPE;

CAR_TABLE := CAR_TABLE_TYPE();
CAR_TABLE.EXTEND(10);
CAR_TABLE(1)(1) := 'DODGE';
CAR_TABLE(2)(1) := 'FORD';
CAR_TABLE(3)(1) := 'MUSTANG';
CAR_TABLE(4)(1) := 'EDSEL';
CAR_TABLE(5)(1) := 'STUDEBAKER';

DBMS_OUTPUT.put_line( '1 ' || CAR_TABLE(1)(1) );
DBMS_OUTPUT.put_line( '2 ' || CAR_TABLE(2)(1) );
DBMS_OUTPUT.put_line( '3 ' || CAR_TABLE(3)(1) );
DBMS_OUTPUT.put_line( '4 ' || CAR_TABLE(4)(1) );
DBMS_OUTPUT.put_line( '5 ' || CAR_TABLE(5)(1) );

When I run I get the following error:

ORA-06531: Reference to uninitialized collection

3
  • A two dimension array mathematically is just a mapping (X, Y) -> VALUE. So, how about creating a temporary table with 3 columns: X, Y, VALUE in PL/SQL ? Commented Feb 6, 2012 at 5:00
  • hi BycycleDude, thanks for your reply. However I cannot create temporary table I think because the columns needs to be dynamic so it can change its column types and number of columns.
    – Jemru
    Commented Feb 6, 2012 at 5:23
  • Easy, just make the create a TYPE column and create numerous nullable VALUE columns and only set the one you need: X, Y, TYPE, VALUE_FLOAT, VALUE_STRING, VALUE_TIMESTAMP, VALUE_BLOB, ... Commented Feb 6, 2012 at 5:52

3 Answers 3

22

Here's an example of using an multidimensional array in pl/sql. Here I use an array containing an array.

declare

  type t_features is table of varchar(100) index by pls_integer;
  type t_car_rec is record
  (
    make varchar2(50),
    model varchar2(50),
    features t_features
  );

  type t_car_tab is table of t_car_rec index by pls_integer;
  car_tab t_car_tab;

  procedure show_detail is
    car_idx pls_integer;
    features_idx pls_integer;
  begin
    car_idx := car_tab.first;
    loop
      exit when car_idx is null;
      dbms_output.put_line('Details for ' || car_tab(car_idx).make || ' ' || car_tab(car_idx).model);

      features_idx := car_tab(car_idx).features.first;
      loop
        exit when features_idx is null;
        dbms_output.put_line('   =>' || car_tab(car_idx).features(features_idx));

        features_idx := car_tab(car_idx).features.next(features_idx);
      end loop;

      car_idx := car_tab.next(car_idx);
    end loop;
  end;

begin

  -- using sequential index values
  car_tab(1).make := 'Ferrari';
  car_tab(1).model := 'Testarossa';
  car_tab(1).features(1) := 'Fast';
  car_tab(1).features(2) := 'Looks cool';
  car_tab(1).features(3) := 'Expensive';

  -- using random index values (sparse)
  car_tab(2).make := 'Acura';
  car_tab(2).model := 'TSX';
  car_tab(2).features(14) := 'Small';
  car_tab(2).features(200) := 'Good MPG';
  car_tab(2).features(36) := 'Inexpensive';

  show_detail;

end;

Output would be:

Details for Ferrari Testarossa
  =>Fast
  =>Looks cool
  =>Expensive 
Details for Acura TSX
  =>Small
  =>Inexpensive
  =>Good MPG

Hope that helps

10

VARRAY and nested table user-defined datatypes always have to be initialized using a constructor. You're doing that correctly for the nested table, but not for the VARRAYs that it contains. The simplest fix is to call the constructor in the assignment lines:

declare
    Type CAR_TABLE_ARRAY is varray(2) of varchar2(255);
    TYPE CAR_TABLE_TYPE IS TABLE OF CAR_TABLE_ARRAY;
    CAR_TABLE CAR_TABLE_TYPE;
begin    
    CAR_TABLE := CAR_TABLE_TYPE();
    CAR_TABLE.EXTEND(10);
    CAR_TABLE(1) := CAR_TABLE_ARRAY('DODGE',null);
    CAR_TABLE(2) := CAR_TABLE_ARRAY('FORD',null);
    CAR_TABLE(3) := CAR_TABLE_ARRAY('MUSTANG',null);
    CAR_TABLE(4) := CAR_TABLE_ARRAY('EDSEL',null);
    CAR_TABLE(5) := CAR_TABLE_ARRAY('STUDEBAKER',null);

    DBMS_OUTPUT.put_line( '1 ' || CAR_TABLE(1)(1) );
    DBMS_OUTPUT.put_line( '2 ' || CAR_TABLE(2)(1) );
    DBMS_OUTPUT.put_line( '3 ' || CAR_TABLE(3)(1) );
    DBMS_OUTPUT.put_line( '4 ' || CAR_TABLE(4)(1) );
    DBMS_OUTPUT.put_line( '5 ' || CAR_TABLE(5)(1) );
end;
5

That's because you're referencing the inner arrays which are not initialized.
Either add something like:

CAR_TABLE(1) := CAR_TABLE_ARRAY();
CAR_TABLE(1).EXTEND(1);
CAR_TABLE(2) := CAR_TABLE_ARRAY();
CAR_TABLE(2).EXTEND(1);
...

Or make the inner arrays (CAR_TABLE_ARRAY) as asociative arrays:

Type CAR_TABLE_ARRAY is TABLE of varchar2(255) index by binary_integer;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.