【问题标题】:I have created the below table stafftable but when I'm trying to insert it's showing an error我已经创建了下表 stafftable 但是当我尝试插入它时显示错误
【发布时间】:2022-11-20 03:31:19
【问题描述】:
CREATE TYPE accounttype AS OBJECT( no varchar2(10), name varchar2(10), balance number(10), dob date, member function age return number );

CREATE TYPE BODY accounttype AS
MEMBER FUNCTION age RETURN NUMBER
AS
BEGIN
RETURN FLOOR(MONTHS_BETWEEN(sysdate,dob)/12);
END age;
END;
/

CREATE TYPE account_branchtype AS OBJECT( account REF accounttype, branch  varchar2(10) );

create type account_branchtabletype as table of account_branchtype;

create type stafftype as object(staff_id varchar2(20),name varchar2(20) ,sal number(20), other_details varchar2(20) , emp8 account_branchtabletype ,dob date , member function getage return number);

create or replace type body stafftype as member function getage return number
as
begin
return(round((sysdate-dob)/365));
end getage;
end;
/

create table stafftable of stafftype nested table emp8 store as relaccount_branch8;

insert into stafftable values(stafftype('S01','Captain','account',20000,'abc','24-apr-1993'));
insert into stafftable values(stafftype('S02','Thor','manager',30000,'pqr','14-jun-1993'));

insert into account_branchtable values('B01','manager','andheri',stafftabletype(stafftype('S01','Captain','account',20000,'abc','24-apr-1993')));
insert into account_branchtable values('B02','asst manager','sion',stafftabletype(stafftype('S02','Thor','manager',30000,'pqr','14-jun-1993')));

当我尝试将数据插入 Stafftable 时,显示错误为 inconsistent datatypes: expected schema.ACCOUNT_BRANCHTABLETYPE got CHAR

小提琴 = https://dbfiddle.uk/zDdqEJdx

【问题讨论】:

  • 请不要close a question 然后再次有效地提出相同的问题(信息略少)。
  • Sadiq——这里的观众对从事浪费读者时间的行为的新用户非常敏感。

标签: sql oracle object-oriented-database


【解决方案1】:

您没有表account_branchtable(您可能不想要它)。


您可能想要创建一个REFs 的集合:

create type account_branchtabletype as table of REF account_branchtype;

然后创建您的员工类型:

create type stafftype as object(
  staff_id      varchar2(20),
  name          varchar2(20),
  sal           number(20),
  other_details varchar2(20),
  emp8          account_branchtabletype,
  dob           date,
  member function getage return number
);

create or replace type body stafftype as member function getage return number
as
begin
return FLOOR(MONTHS_BETWEEN(sysdate,dob)/12);
end getage;
end;
/

然后创建表:

create table stafftable of stafftype (
  staff_id PRIMARY KEY
) nested table emp8 store as relaccount_branch8;

ALTER TABLE relaccount_branch8 ADD SCOPE FOR (COLUMN_VALUE) IS account_branch;

然后您可以插入具有嵌套表值的人员:

insert into stafftable (staff_id, name, sal, other_details, emp8, dob)
values(
  'S01',
  'Captain',
  20000,
  'abc',
  account_branchtabletype(
    (SELECT REF(b) FROM account_branch b WHERE b.account.no = '19DCS001' AND b.branch = 'Manjalpur')
  ),
  DATE '1993-04-24'
);

fiddle

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-15
    • 2022-10-25
    • 2019-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多