tags:

views:

1607

answers:

2

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.

A: 

Those are PL/SQL types as the error suggests. There's nothing stopping you from declaring them in the public specification of a package, then you can refer to them in any PL/SQL code you need.

Jeffrey Kemp
Doesn't look like packages allow object type definitions:create or replacepackage my_package as type my_type_map is table of varchar2(10) index by varchar2(10); type my_other_type as object ( id number(15), member procedure initialize(p_my_type_map number) ) not instantiable not final;end;Error(2,1): PLS-00707: unsupported construct or internal error [2603]Error(5,3): PLS-00540: object not supported in this context.Error(5,3): PL/SQL: Declaration ignoredAm I going about this the wrong way?
jlpp
I think what he is saying is that you should be declaring the object and the pl/sql type apart from each other
moleboy
Unfortuantely an object type defined outside of PL/SQL can not reference anything in PL/SQL.
jlpp
I presume you mean an "object type defined outside of a PL/SQL package" - what do you mean by it cannot reference anything in PL/SQL?"Object types and subtypes can be used in PL/SQL procedures and functions in most places where built-in types can appear. The parameters and variables of PL/SQL functions and procedures can be of object types. You can implement the methods associated with object types in PL/SQL. These methods (functions and procedures) reside on the server as part of a user's schema." http://download.oracle.com/docs/cd/B19306_01/appdev.102/b14260/adobjenv.htm#sthref552
Jeffrey Kemp
Yes, that it was I meant. Thanks for the clarification. As far as I can tell I was stuck because I couldn't define the associative array type outside of a package and I couldn't define the dependent object type inside of a package. So I decided to abandon the object type approach altogether and use Oracle record types.
jlpp
Sounds like a good idea :)
Jeffrey Kemp
+2  A: 

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 taa))
                               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.

DCookie
Thanks DCookie. I've decided against using Oracle object types and am going with straight PL/SQL and record types.
jlpp
I'm not a big fan of Oracle Objects - I don't see the huge benefit, and they're absolutely nonstandard, making your data structures non-portable.
DCookie