创建或替换全局子类型

Create or replace global subtype

我知道我可以在包规范中创建子类型,例如:

CREATE OR REPLACE PACKAGE XY
AS
  SUBTYPE type_sdebug IS VARCHAR (200);
  ...
END;
/

如果我想在另一个包中使用相同的子类型,那么我需要再次重新定义相同的类型。有没有办法创建或替换全局子类型,例如:

CREATE OR REPLACE TYPE STRING_ARRAY AS VARRAY(500) OF VARCHAR2(30);
/

据我所知,SUBTYPE 是一项 PL/SQL 功能,因此您无法在全局范围内创建它们。但是没有什么可以阻止您在另一个包中使用包 XY 中定义的类型(例如 AB):

CREATE OR REPLACE PACKAGE XY
AS
  SUBTYPE type_sdebug IS VARCHAR (200);
END;

CREATE OR REPLACE PACKAGE AB
AS
  PROCEDURE print_it(p_Debug in XY.type_sdebug);
END;

CREATE OR REPLACE PACKAGE BODY AB
AS
  PROCEDURE print_it(p_Debug in XY.type_sdebug) is
  begin
      dbms_output.put_line(p_Debug);
  end;
END;


declare
  v_Debug XY.type_sdebug default 'hello world';
begin
  ab.print_it(v_Debug);
end;

现在我已经正确阅读了问题 *{;-) ,根据 the documentation 你确实可以创建一个子类型:

"This statement shows how the subtype corporate_customer_typ in the sample oe schema was created. It is based on the customer_typ supertype created in the preceding example and adds the account_mgr_id attribute. A hypothetical name is given to the table so that you can duplicate this example in your test database:"

CREATE TYPE corporate_customer_typ_demo UNDER customer_typ
    ( account_mgr_id     NUMBER(6)
    );

不过,根据 prerequisites for creating types:

,您可能需要额外的权限才能执行此操作

"To create a subtype, you must have the UNDER ANY TYPE system privilege or the UNDER object privilege on the supertype."