在 Oracle 12c 中插入对象数据

Posted

技术标签:

【中文标题】在 Oracle 12c 中插入对象数据【英文标题】:Insert Object data in Oracle 12c 【发布时间】:2017-01-09 18:31:11 【问题描述】:

我正在尝试在其中一列是 Object 类型的表中插入数据,但出现以下错误:

RA-06553:PLS-306:调用“SET_DATUME”时参数的数量或类型错误

表定义是:

CREATE TABLE Trebovanje (
BrDokumenta NUMBER,
Datumi DATUMI_T,
SifRJ NUMBER,
BrRadKnjiz NUMBER,
CONSTRAINT trebovanje_pk PRIMARY KEY (BrDokumenta),
CONSTRAINT treb_rj_fk FOREIGN KEY (SifRJ) REFERENCES RadnaJedinica (SifRJ),
CONSTRAINT trebovanje_zap_fk FOREIGN KEY (BrRadKnjiz) REFERENCES Zaposleni (BrRadKnjiz)
);

DATUMI_T 用户定义类型:

create or replace TYPE DATUMI_T AS OBJECT  ( 
DatPrijema DATE,   
DatStampanja DATE,
MEMBER FUNCTION get_dat_prijema RETURN DATE,
MEMBER FUNCTION get_dat_stampanja RETURN DATE,
MEMBER PROCEDURE set_datume(p_dat_prijema DATE, p_dat_stampanja DATE),
MEMBER FUNCTION proveri_dat_stampanja(p_dat_stampanja DATE) RETURN BOOLEAN
);

create or replace TYPE BODY DATUMI_T AS

  MEMBER FUNCTION get_dat_prijema RETURN DATE AS   
  BEGIN
    RETURN self.DatPrijema;   
  END get_dat_prijema;

  MEMBER FUNCTION get_dat_stampanja RETURN DATE AS   
  BEGIN
    RETURN self.DatStampanja;   
  END get_dat_stampanja;

  MEMBER PROCEDURE set_datume(p_dat_prijema DATE, p_dat_stampanja DATE) AS   
  BEGIN
    self.DatPrijema := p_dat_prijema;

    IF proveri_dat_stampanja(p_dat_stampanja)
    THEN
      self.DatStampanja := p_dat_stampanja;
    ELSE
      self.DatStampanja := SYSDATE;
    END IF;   
  END set_datUME;

  MEMBER FUNCTION proveri_dat_stampanja(p_dat_stampanja DATE) RETURN BOOLEAN AS   
  ok BOOLEAN;   
  BEGIN    
    ok := self.DatPrijema < p_dat_stampanja;
    RETURN ok;   
  END proveri_dat_stampanja;
END;

还有我的插入声明

insert into trebovanje values (
  1, 
  DATUMI_T.set_datume(
    TO_DATE('2017-01-02 17:44:33', 'YYYY-MM-DD HH24:MI:SS'), 
    TO_DATE('2017-01-02 17:55:33', 'YYYY-MM-DD HH24:MI:SS')
    ),
  1,
  1234);

对此有什么想法吗?

【问题讨论】:

【参考方案1】:

您似乎需要定义一个“隐藏”默认构造函数的构造函数。

CREATE OR REPLACE TYPE DATUMI_T AS OBJECT
(
    DatPrijema DATE,
    DatStampanja DATE,
    MEMBER FUNCTION get_dat_prijema
        RETURN DATE,
    MEMBER FUNCTION get_dat_stampanja
        RETURN DATE,
    MEMBER PROCEDURE set_datume(p_dat_prijema DATE, p_dat_stampanja DATE),
    MEMBER FUNCTION proveri_dat_stampanja(p_dat_stampanja DATE)
        RETURN BOOLEAN,
    /* the declaration of your own constructor ( same name of the type ) */
    CONSTRUCTOR FUNCTION DATUMI_T(DatPrijema DATE, DatStampanja DATE)
        RETURN SELF AS RESULT
);
create or replace TYPE BODY DATUMI_T AS

  MEMBER FUNCTION get_dat_prijema RETURN DATE AS   
  BEGIN
    RETURN self.DatPrijema;   
  END get_dat_prijema;

  MEMBER FUNCTION get_dat_stampanja RETURN DATE AS   
  BEGIN
    RETURN self.DatStampanja;   
  END get_dat_stampanja;

  MEMBER PROCEDURE set_datume(p_dat_prijema DATE, p_dat_stampanja DATE) AS   
  BEGIN
    self.DatPrijema := p_dat_prijema;

    IF proveri_dat_stampanja(p_dat_stampanja)
    THEN
      self.DatStampanja := p_dat_stampanja;
    ELSE
      self.DatStampanja := SYSDATE;
    END IF;   
  END set_datUME;

  /* the body of your constructor */
  CONSTRUCTOR FUNCTION DATUMI_T(DatPrijema DATE, DatStampanja DATE)
    RETURN SELF AS RESULT as  
  BEGIN
    self.DatPrijema := DatPrijema;

    IF proveri_dat_stampanja(DatStampanja)
    THEN
      self.DatStampanja := DatStampanja;
    ELSE
      self.DatStampanja := SYSDATE;
    END IF;   
    return;
  END DATUMI_T;

  MEMBER FUNCTION proveri_dat_stampanja(p_dat_stampanja DATE) RETURN BOOLEAN AS   
  ok BOOLEAN;   
  BEGIN    
    ok := self.DatPrijema < p_dat_stampanja;
    RETURN ok;   
  END proveri_dat_stampanja;
END;

【讨论】:

以上是关于在 Oracle 12c 中插入对象数据的主要内容,如果未能解决你的问题,请参考以下文章

ORACLE 12C 插入数据遇到 Error getting generated key or setting result to parameter object错误

ORACLE 12C 插入数据遇到 Error getting generated key or setting result to parameter object错误

在 oracle 数据库 12c 中删除对象而不生成归档日志

ORACLE 12C切换至可插入式数据库命令

ORACLE 12C切换至可插入式数据库命令

本机安装oracle12C