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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1069176/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 01:59:18  来源:igfitidea点击:

Possible to create Oracle Database object types inside of PL/SQL?

oracleplsql

提问by jlpp

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

是否可以在 Oracle 数据库 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

谢谢,杰夫

回答by l0b0

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.

所以,不幸的是,没有。

回答by user272735

Update for Oracle 11g Release 2:

Oracle 11g 第 2 版更新:

From Chapter 3 Using PL/SQL With Object Typesof 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.

  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.

在 PL/SQL 块、子程序或包中使用对象类型是一个两步过程。

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

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

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

回答by Theo

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