在多层嵌套表中插入时如何解决“ ORA-01401:插入值对于列而言太大”

时间:2019-05-19 10:12:28

标签: oracle oracle11g user-defined-types object-relational-model ordbms

下面是表/对象类型创建查询。表创建成功

CREATE TYPE ft_obj AS OBJECT (
    ftid         NUMBER(5),
    ftlocation   VARCHAR(30),
    country      VARCHAR(10)
);
/

CREATE TABLE ft_table OF ft_obj (
    ftid PRIMARY KEY
) OBJECT IDENTIFIER IS PRIMARY KEY;
/

CREATE TYPE frod_obj AS OBJECT (
    prodid           NUMBER(6),
    ft_ref           ft_obj,
    proddesc         VARCHAR(50),
    costperitem      DECIMAL,
    labcostperitem   DECIMAL
);
/

CREATE TABLE frod_table OF frod_obj (
    prodid PRIMARY KEY
) OBJECT IDENTIFIER IS PRIMARY KEY;

CREATE TYPE wf_obj AS OBJECT (
    wfid           NUMBER,
    ft_ref         ft_obj,
    wfname         VARCHAR(30),
    taxcode        INT,
    yearlyincome   DECIMAL,
    yearlytax      DECIMAL
);
/

CREATE TABLE wf_table OF wf_obj (
    wfid PRIMARY KEY
) OBJECT IDENTIFIER IS PRIMARY KEY;
/

CREATE TYPE wfusage_obj AS OBJECT (
    jobdate         DATE,
    jobhours        INT,
    jobhourlyrate   DECIMAL,
    jobposted       CHAR,
    wfid_ref        REF wf_obj
);
/

CREATE TYPE wfusage_nesttabtyp AS
    TABLE OF wfusage_obj;
/

CREATE TABLE wfusage_objtab OF wfusage_obj;
/

CREATE TYPE odetails_obj AS OBJECT (
    mfid           NUMBER,
    prodid_ref     REF frod_obj,
    quantity       INT,
    itemprice      DECIMAL,
    wfusage_ntab   wfusage_nesttabtyp
);
/

CREATE TYPE odetails_nesttabtyp AS
    TABLE OF odetails_obj;
/

CREATE TYPE prod_obj AS OBJECT (
    prodoid          NUMBER,
    odate            DATE,
    promisedate      DATE,
    completiondate   DATE,
    shipmentdate     DATE,
    status           VARCHAR(20),
    odetails_ntab    odetails_nesttabtyp
);
/

CREATE TABLE prod_objtab OF prod_obj (
    PRIMARY KEY ( prodoid )
) OBJECT IDENTIFIER IS PRIMARY KEY
NESTED TABLE odetails_ntab STORE AS oprod_ntab ( (
    PRIMARY KEY ( nested_table_id,
                  mfid )
)
ORGANIZATION INDEX
COMPRESS 
NESTED TABLE wfusage_ntab STORE AS wforder_ntab
)
RETURN AS LOCATOR
/

ALTER TABLE oprod_ntab ADD (
    SCOPE FOR ( prodid_ref ) IS frod_table
);
/

在嵌套表中插入数据时出现错误ORA-01401:插入值对于列而言太大。下面是插入查询

INSERT INTO prod_objtab VALUES (
 46000,
 '25-April-2019',
 '12-May-2019',
 '13-May-2019',
  '13-May-2019',
  'COMPLETED',
  odetails_nesttabtyp(
      odetails_obj(46001
                    ,(SELECT REF(pt) 
                          FROM frod_table pt
                          WHERE pt.prodid = 10002)
                    ,100
                    ,400
                   ,wfusage_nesttabtyp(
                     wfusage_obj('25-April-2019'
                              ,60
                              ,100
                              ,'AME',
                             (SELECT REF(wf) 
                              FROM wf_table wf
                              WHERE wf.wfid = 252)
                    )
                )
              ) 
            )
          )

在第9行获取错误

  

ORA-01401:插入值对于列而言太大

1 个答案:

答案 0 :(得分:1)

花了一个多小时来调试它,我很随意地说,这是一个多么可怕的数据模型。像这样的嵌套对象说明了OO作为管理数据方法的固有问题。

无论如何,问题是这样的(不可避免的是我看的最后一件事):

  wfusage_obj('25-April-2019'
                          ,60
                          ,100
                          ,'AME', <------------ culprit
                         (SELECT REF(wf) 
                          FROM wf_table wf
                          WHERE wf.wfid = 252)
                )

'AME'长三个字符。这正在填充wfusage_obj.jobposted,您已将其定义为CHAR。合法的语法,但是如果我们不指定数据长度,则默认为CHAR(1)。显然,这是'AME'的两个字符的缩写。它必须是(至少)CHAR(3)。

解决此问题后,您将绊倒

  

ORA-22979:无法插入对象视图REF或用户定义的REF

这是因为您已使用OBJECT IDENTIFIER IS PRIMARY KEY定义了表。因此,您不能在这些表上使用REF。因此,您需要从表定义中删除该代码以使代码正常工作。

我已经发布了a demo on db<>fiddle