Creating or simulating two dimensional arrays in PL/SQL

后端 未结 3 1129
悲哀的现实
悲哀的现实 2020-12-06 04:49

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 ap

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-06 05:50

    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;
    

提交回复
热议问题