在继承中检索对象属性的Oracle查询

ggazkfy8  于 2023-04-11  发布在  Oracle
关注(0)|答案(1)|浏览(135)

我把类型和子类型定义为

Create or replace type Component_ty as object (
   Code VARCHAR(10),
   Component_Description VARCHAR(100),
   Component_Type VARCHAR(10))
   NOT FINAL;

   Create or replace type External_Component_ty under Component_ty(
   External_Company REF External_Company_ty);

其中External_company_ty定义为

Create or replace type External_Company_ty as object (
   VAT VARCHAR(15),
   Company_Name VARCHAR(20),
   Component_code VARCHAR(5),
   Address External_Company_address_ty)
   NOT FINAL;

table是

create table Component of component_ty(
   Code Primary Key, 
   Component_Description NOT NULL,
   Component_Type NOT NULL);

create table External_Company of external_company_ty (
   VAT Primary Key,
   Company_Name NOT NULL,
   Component_Code NOT NULL,
   Address NOT NULL);

我可以向这个查询添加什么,以便显示另一列,其中external_company.component_code引用了该特定组件??

select c.*, treat(value(c) as external_component_ty).external_company as external_company FROM component c
ac1kyiln

ac1kyiln1#

只需选择component_code属性:

select c.*,
       treat(value(c) as external_component_ty).external_company.component_code
         as external_company_comp_code
FROM   component c

对于示例数据(忽略address列,因为您尚未定义该对象):

INSERT INTO external_company VALUES (
  external_company_ty('A', 'Comp A', 'CodeA')
);

INSERT INTO component VALUES (
  external_component_ty(
    'A',
    'ABC',
    'XYZ',
    (SELECT REF(ec) FROM external_company ec WHERE vat = 'A')
 )
);

其输出:
| 代码|组件描述|组件类型|外部公司代码|
| --------------|--------------|--------------|--------------|
| A|ABC|XYZ|CodeA|
fiddle

相关问题