可以在PL / SQL中创建Oracle数据库对象类型吗?

时间:2022-03-09 12:52:32

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

是否可以在Oracle Database 10g中的包内创建对象类型?就像是:

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.

错误(3,9):PLS-00540:此上下文中不支持该对象。

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.

我最终希望能够做的是使用多态,但也允许对象访问表并使用PL / SQL,这在包外定义的类型中是不允许的。

Thanks, Jeff

3 个解决方案

#1


From the Oracle 10g documentation:

从Oracle 10g文档:

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

目前,您无法在PL / SQL块,子程序或包中定义对象类型。

So, unfortunately, no.

所以,不幸的是,没有。

#2


Update for Oracle 11g Release 2:

Oracle 11g第2版的更新:

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

从第3章到使用PL / SQL与Oracle数据库对象类型的对象类型 - 关系开发人员指南:

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

在PL / SQL块,子程序或包中使用对象类型分为两步。

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

    您必须使用SQL语句CREATE TYPE,SQL * Plus或其他类似程序来定义对象类型。

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

    在模式中定义并安装对象类型后,可以在任何PL / SQL块,子程序或包中使用它。

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

    在PL / SQL中,然后声明一个变量,其数据类型是您刚刚定义的用户定义类型或ADT。

#3


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

您可以在PL / SQL包之外定义的对象中使用PL / SQL!使用对象体:

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

从以下链接复制粘贴此示例:http://www.mcs.csueastbay.edu/support/oracle/doc/10.2/appdev.102/b14260/adobjint.htm

#1


From the Oracle 10g documentation:

从Oracle 10g文档:

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

目前,您无法在PL / SQL块,子程序或包中定义对象类型。

So, unfortunately, no.

所以,不幸的是,没有。

#2


Update for Oracle 11g Release 2:

Oracle 11g第2版的更新:

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

从第3章到使用PL / SQL与Oracle数据库对象类型的对象类型 - 关系开发人员指南:

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

在PL / SQL块,子程序或包中使用对象类型分为两步。

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

    您必须使用SQL语句CREATE TYPE,SQL * Plus或其他类似程序来定义对象类型。

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

    在模式中定义并安装对象类型后,可以在任何PL / SQL块,子程序或包中使用它。

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

    在PL / SQL中,然后声明一个变量,其数据类型是您刚刚定义的用户定义类型或ADT。

#3


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

您可以在PL / SQL包之外定义的对象中使用PL / SQL!使用对象体:

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

从以下链接复制粘贴此示例:http://www.mcs.csueastbay.edu/support/oracle/doc/10.2/appdev.102/b14260/adobjint.htm