Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to create Oracle associative array type outside of a package/procedure?

Tags:

oracle

plsql

In Oracle Database 10g, is it possible to create an associative array type outside of a package or procedure? I would like to be able to do this so that I can reference this associative array type in another type. For example:

create type my_type_map is table of varchar2(10) index by varchar2(10);

create type my_other_type as object (   
    id number(15),
    member procedure initialize(p_my_type_map my_type_map)
) not instantiable not final;

The error I get is:

SQL> create type my_type_map is table of varchar2(20) index by varchar2(10);
  2  /

Warning: Type created with compilation errors.

SQL> show errors;
Errors for TYPE MY_TYPE_MAP:

LINE/COL ERROR
-------- -----------------------------------------------------------------
0/0      PL/SQL: Compilation unit analysis terminated
1/21     PLS-00355: use of pl/sql table not allowed in this context
SQL> 

Seems that Oracle considers:

index by varchar2(10)    

to be PL/SQL and doens't allow it in the creation of SQL types. If Oracle really doesn't allow associative arrays to be defined outside of packages then is there a good alternative? Is it possible to create types that extend Oracle's Object inside of a package so that all types are defined in the same package?

Thanks, Jeff

Edit: Corrected code sample, added log, added possible alternative as question.

like image 369
jlpp Avatar asked Jun 30 '09 22:06

jlpp


1 Answers

The answer is no, you cannot do what you're trying to do, any more than you can create a type to add a BOOLEAN typed variable to an object. The items in an object must contain Oracle types, not PL/SQL types. A bit clunky alternative could be:

CREATE TYPE t_aa AS VARRAY(10) OF VARCHAR2(10);

CREATE OR REPLACE TYPE t_ua AS OBJECT (ID NUMBER(15)
                                     , MEMBER PROCEDURE initialize(p_aa t_aa)
                                     , MEMBER PROCEDURE initialize(p_aa_i t_aa))
                               NOT INSTANTIABLE NOT FINAL;

Store your associated pairs of variables in the two VARRAYs. You will have to know the largest possible size of your arrays.

like image 132
DCookie Avatar answered Nov 08 '22 05:11

DCookie