25

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
  • This still doesn't work in oracle 11.2.... This is possible in both Pascal and ADA, ancestors of pl/sql; so I thought we will get there quickly. Commented Jun 13, 2017 at 10:01

3 Answers 3

27

From the Oracle 10g documentation:

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

So, unfortunately, no.

Sign up to request clarification or add additional context in comments.

1 Comment

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.
10

Update for Oracle 11g Release 2:

From Chapter 3 Using PL/SQL With Object Types of Oracle Database Object-Relational Developer's Guide:

Using object types in a PL/SQL block, subprogram, or package is a two-step process.

  1. You must define object types using the SQL statement CREATE TYPE, in SQL*Plus or other similar programs.

    After an object type is defined and installed in the schema, you can use it in any PL/SQL block, subprogram, or package.

  2. In PL/SQL, you then declare a variable whose data type is the user-defined type or ADT that you just defined.

1 Comment

The original question is about "creating" objects inside package. Your reply is about "using" objects inside plsql block/packages... It's very different topic.
6

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

1 Comment

I know it's 9 years old, but man, this doesn't address the question.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.