Oracle 中的子类型参考

发布于 2024-10-07 07:30:24 字数 1186 浏览 2 评论 0原文

请帮忙,问题在底部。

CREATE OR REPLACE TYPE NameType AS OBJECT 
(firstName VARCHAR(20), middleName VARCHAR(20), lastName VARCHAR(20))
INSTANTIABLE
FINAL;

CREATE OR REPLACE TYPE ServicerType AS OBJECT 
(servicerID INTEGER, servicerType VARCHAR(10))
INSTANTIABLE
FINAL;

CREATE OR REPLACE TYPE PersonType AS OBJECT 
(personID INTEGER, name NameType, gender CHAR(1))
INSTANTIABLE
NOT FINAL;

CREATE OR REPLACE TYPE EmployeeType UNDER PersonType 
(servicerRef REF ServicerType)
NOT INSTANTIABLE
NOT FINAL;

CREATE OR REPLACE TYPE FullTimeEmployeeType UNDER EmployeeType 
(yearlySalary NUMBER(19,4))
INSTANTIABLE
FINAL;

CREATE TABLE Servicer OF ServicerType;
CREATE TABLE Person OF PersonType;

INSERT ALL
INTO Servicer values (ServicerType(100, 'Company')
SELECT * FROM dual

INSERT ALL
INTO Person VALUES (FullTimeEmployeeType(200, 
    NameType('Bob', 'T', 'Builder'), 'M', 
    (select ref(s) from servicer s where servicerid = 100), 56700))
SELECT * FROM dual

现在我想运行此查询:

select p.gender, p.name.firstName, p.name.lastName, p.yearlySalary from person p

我收到错误。我知道yearlySalary位于FullTimeEmployee中,它是person的子类型,但我只是不知道如何引用它。

Please help, the question is at the bottom.

CREATE OR REPLACE TYPE NameType AS OBJECT 
(firstName VARCHAR(20), middleName VARCHAR(20), lastName VARCHAR(20))
INSTANTIABLE
FINAL;

CREATE OR REPLACE TYPE ServicerType AS OBJECT 
(servicerID INTEGER, servicerType VARCHAR(10))
INSTANTIABLE
FINAL;

CREATE OR REPLACE TYPE PersonType AS OBJECT 
(personID INTEGER, name NameType, gender CHAR(1))
INSTANTIABLE
NOT FINAL;

CREATE OR REPLACE TYPE EmployeeType UNDER PersonType 
(servicerRef REF ServicerType)
NOT INSTANTIABLE
NOT FINAL;

CREATE OR REPLACE TYPE FullTimeEmployeeType UNDER EmployeeType 
(yearlySalary NUMBER(19,4))
INSTANTIABLE
FINAL;

CREATE TABLE Servicer OF ServicerType;
CREATE TABLE Person OF PersonType;

INSERT ALL
INTO Servicer values (ServicerType(100, 'Company')
SELECT * FROM dual

INSERT ALL
INTO Person VALUES (FullTimeEmployeeType(200, 
    NameType('Bob', 'T', 'Builder'), 'M', 
    (select ref(s) from servicer s where servicerid = 100), 56700))
SELECT * FROM dual

Now I want to run this query:

select p.gender, p.name.firstName, p.name.lastName, p.yearlySalary from person p

I got an error. I know that yearlySalary is in FullTimeEmployee which is a subtype of person, but I just don't know how to reference it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

池木 2024-10-14 07:30:24

您需要 TREAT 函数

select p.gender, p.name.firstName, p.name.lastName,
        treat(value(p) as FullTimeEmployeeType).yearlySalary
from person p
/

You need the TREAT function

select p.gender, p.name.firstName, p.name.lastName,
        treat(value(p) as FullTimeEmployeeType).yearlySalary
from person p
/
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文