tags:

views:

567

answers:

2

Is it possible to create an object type inside of a package in Oracle Database 10g? Something like:

create or replace package my_package as 
    type my_type as object (
        id number(15) 
     ); 
end;

Gives:

Error(3,9): PLS-00540: object not supported in this context.

What I'm ultimately looking to be able to do is use polymorphism but also allow the objects to access tables and use PL/SQL, which isn't allowed in types defined outside of packages.

Thanks, Jeff

+1  A: 

From the Oracle 10g documentation:

Currently, you cannot define object types in a PL/SQL block, subprogram, or package.

So, unfortunately, no.

l0b0
It is true that you can't define object types in PL/SQL but you can use PL/SQL in an object body defined outside PL/SQL. jlpp makes a wrong assumptation.
Theo
A: 

You can use PL/SQL in objects that are defined outside a PL/SQL package!! Use object bodies:

CREATE TYPE person_typ AS OBJECT (
  idno           NUMBER,
  first_name     VARCHAR2(20),
  last_name      VARCHAR2(25),
  email          VARCHAR2(25),
  phone          VARCHAR2(20),
  MAP MEMBER FUNCTION get_idno RETURN NUMBER, 
  MEMBER PROCEDURE display_details ( SELF IN OUT NOCOPY person_typ ));
/

CREATE TYPE BODY person_typ AS
  MAP MEMBER FUNCTION get_idno RETURN NUMBER IS
  BEGIN
    RETURN idno;
  END;
  MEMBER PROCEDURE display_details ( SELF IN OUT NOCOPY person_typ ) IS
  BEGIN
    -- use the PUT_LINE procedure of the DBMS_OUTPUT package to display details
    DBMS_OUTPUT.PUT_LINE(TO_CHAR(idno) || ' ' || first_name || ' ' || last_name);
    DBMS_OUTPUT.PUT_LINE(email || ' '  || phone);
  END;
END;
/

copy-pasted this example from this link: http://www.mcs.csueastbay.edu/support/oracle/doc/10.2/appdev.102/b14260/adobjint.htm

Theo