这里不允许使用错误列&插入时的值太多

时间:2013-12-21 09:07:24

标签: sql oracle function types

创建Subscriber_T类型的Subscriber表之后我遇到了这个新问题,它的功能似乎无法在Subscrber表中插入任何值。错误,我一直在这里不允许列。但在哪里???

这些是脚本和插入记录。

CREATE OR REPLACE TYPE Surnames_T AS OBJECT (
  Surname varchar (20)
);
/

CREATE OR REPLACE TYPE listSurnames_T 
AS Varray(4) of Surnames_T;
/


CREATE OR REPLACE TYPE listTels as object(
  Tel   number (12)
);
/

CREATE OR REPLACE TYPE listTels_T as Varray(5) of listTels;
/


CREATE OR REPLACE TYPE ADDRESS_T AS OBJECT (
  NUM       number (6),
  STREET    varchar (20),
  TOWN      varchar (20)
);
/

CREATE or replace type TAddress 
as table of Address_T;
/

create or replace type Subscriber_T as object(
  num_s  number(6),
  sName varchar(30), 
  surname listSurnames_T,
  Adds TAddress,
  DateOfBirth  date,
  phoneNo   listTels_T,
  member function Age return number
);
/

create table Subscribers of Subscriber_T(
  CONSTRAINT subscriber_pk primary key (num_s)
)
nested table Adds store as Tab_Adds;
/
show errors


create or replace type body Subscriber_T as
member function Age return number is
calc_age number;
dob date;
diff  date;
begin
  select S.dateOfBirth into dob
  from Subscribers S
  where deref(S.num_s) = self

  diff := sysdate - dob  
  calc_age := to_Char(diff, 'YYYY')

  return cal_age;
end;
    end;
/
show errors


insert into Subscribers values (34, Chloe, listSurnames_T(Surnames_T('Dave'), Surnames_T('Camille'), Surnames_T('Jones')), 
TAddress(Address_T(10, 'ave Foch', 'Ravenwood'), Address_T(30, 'rue des pole', 'England')), 
'10-11-1976', 
listTels_T(listTels(5839550456), listTels(6834734567)));

我猜这个函数正在引发这个问题但它在编译时没有显示错误。

1 个答案:

答案 0 :(得分:2)

一些问题:

  1. 您无需在成员函数SELECT中执行age
  2. 您没有引用名称Chloe,因此它不是字符串。
  3. 您尚未将'10-11-1976'转换为日期。
  4. You should not use VARCHAR as a datatype - 改为使用VARCHAR2
  5. SQL Fiddle

    Oracle 11g R2架构设置

    CREATE OR REPLACE TYPE Surnames_T AS OBJECT (
      Surname varchar2 (20)
    )
    /
    
    CREATE OR REPLACE TYPE listSurnames_T 
    AS Varray(4) of Surnames_T
    /
    
    
    CREATE OR REPLACE TYPE listTels as object(
      Tel   number (12)
    )
    /
    
    CREATE OR REPLACE TYPE listTels_T as Varray(5) of listTels
    /
    
    
    CREATE OR REPLACE TYPE ADDRESS_T AS OBJECT (
      NUM       number (6),
      STREET    varchar2 (20),
      TOWN      varchar2 (20)
    )
    /
    
    CREATE or replace type TAddress as table of Address_T
    /
    
    create or replace type Subscriber_T as object(
      num_s        number(6),
      sName        varchar2(30), 
      surname      listSurnames_T,
      Adds         TAddress,
      DateOfBirth  date,
      phoneNo       listTels_T,
      member function Age return number
    )
    /
    
    
    create or replace type body Subscriber_T as
      member function Age return number is
      begin
        return FLOOR( MONTHS_BETWEEN( SYSDATE, self.DateOfBirth )/12 );
      end;
    end;
    /
    
    create table Subscribers of Subscriber_T(
      CONSTRAINT subscriber_pk primary key (num_s)
    )
    nested table Adds store as Tab_Adds
    /
    
    insert into Subscribers values (
      34,
      'Chloe',
      listSurnames_T(
        Surnames_T('Dave'),
        Surnames_T('Camille'),
        Surnames_T('Jones')
      ), 
      TAddress(
        Address_T(10, 'ave Foch', 'Ravenwood'),
        Address_T(30, 'rue des pole', 'England')
      ),
      TO_DATE( '10-11-1976', 'DD-MM-YYYY' ), 
      listTels_T(
        listTels(5839550456),
        listTels(6834734567)
      )
    )
    /
    

    查询1

    SELECT s.age() FROM subscribers s
    

    <强> Results

    | S.AGE() |
    |---------|
    |      37 |
    

    修改

    我不确定您为什么要在VARCHAR2类型中包含Surnames_T或在NUMBER类型中包含ListTels。您可以省略这些,只需执行以下操作:

    CREATE OR REPLACE TYPE listSurnames_T 
    AS Varray(4) of VARCHAR2(20)
    /
    
    CREATE OR REPLACE TYPE listTels_T as Varray(5) of NUMBER(12)
    /
    

    insert into Subscribers values (
      34,
      'Chloe',
      listSurnames_T(
        'Dave',
        'Camille',
        'Jones'
      ), 
      TAddress(
        Address_T(10, 'ave Foch', 'Ravenwood'),
        Address_T(30, 'rue des pole', 'England')
      ),
      TO_DATE( '10-11-1976', 'DD-MM-YYYY' ), 
      listTels_T(
        5839550456,
        6834734567
      )
    )
    /