oracle 在db< >fiddle会话中引用用户定义的类型(插入行时)

yrefmtwq  于 2023-11-17  发布在  Oracle
关注(0)|答案(1)|浏览(130)

在db<>fiddle for Oracle 18 c中:
我创建了一个用户定义类型(UDT)和一个使用该类型的表:

create or replace TYPE "MY_ST_GEOMETRY" Authid current_user  AS object
  (entity integer,numpts integer,minx float(64),
  miny float(64),maxx float(64),maxy float(64),
  minz float (64),maxz float(64),minm float(64),
  maxm float(64),area float(64),len float(64),
  srid integer,points blob,
  constructor Function my_st_geometry(geom_str clob,srid number) Return self AS result deterministic,
  constructor Function my_st_geometry(x     number,
                                   y     number,
                                   z     number,
                                   m     number,
                                   srid  integer) Return self AS result deterministic) NOT final

create table polygons (
    id int,
    shape my_st_geometry
)

字符串
我想在表中插入一行。

insert into polygons (id, shape ) VALUES (
 1,
 my_st_geometry('polygon ((52 28,58 28,58 23,52 23,52 28))', 4326)
);


但我得到一个错误:

ORA-04067: not executed, type body "FIDDLE_KVDCLIHQNZSONDPDJHLS.MY_ST_GEOMETRY" does not exist
ORA-06508: PL/SQL: could not find program unit being called: "FIDDLE_KVDCLIHQNZSONDPDJHLS.MY_ST_GEOMETRY"
ORA-06512: at line 1


db<>fiddle
我怀疑我需要在UDT前面加上用户名,因为每次我点击run时db<>fiddle都会生成一个不同的用户名,所以我相信我需要使用一个函数动态地获取用户名。
有没有一个函数可以用来获取用户--这样我就可以在插入一行时给UDT加上前缀?

kyxcudwk

kyxcudwk1#

你需要将类型传递给构造函数:

create or replace TYPE "MY_ST_GEOMETRY" Authid current_user  AS object(
  entity integer,
  numpts integer,
  minx float(64),
  miny float(64),
  maxx float(64),
  maxy float(64),
  minz float(64),
  maxz float(64),
  minm float(64),
  maxm float(64),
  area float(64),
  len float(64),
  srid integer,
  points blob,

  constructor Function my_st_geometry(
    SELF IN OUT NOCOPY my_st_geometry,
    geom_str clob,
    srid number
  ) Return self AS result deterministic,
  
  constructor Function my_st_geometry(
    SELF IN OUT NOCOPY my_st_geometry,
    x     number,
    y     number,
    z     number,
    m     number,
    srid  integer
  ) Return self AS result deterministic
) NOT final;

字符串
然后你需要为类型创建一个主体,定义你想要如何构造类型:

create or replace TYPE BODY "MY_ST_GEOMETRY" IS
  constructor Function my_st_geometry(
    SELF IN OUT NOCOPY my_st_geometry,
    geom_str clob,
    srid number
  ) Return self AS result deterministic
  IS
  BEGIN
    -- Add constructor logic here.
    RETURN;
  END;
  
  constructor Function my_st_geometry(
    SELF IN OUT NOCOPY my_st_geometry,
    x     number,
    y     number,
    z     number,
    m     number,
    srid  integer
  ) Return self AS result deterministic
  IS
  BEGIN
    -- Add constructor logic here.
    SELF.minx := x;
    SELF.maxx := x;
    SELF.miny := y;
    SELF.maxy := y;
    SELF.minz := z;
    SELF.maxz := z;
    SELF.minm := m;
    SELF.maxm := m;
    SELF.srid := srid;
    RETURN;
  END;
END;
/


然后它就“工作”了(注意:您需要完成构造函数并定义构造类型的逻辑)。
fiddle

相关问题