oracle 打印有关嵌套表中的子类型的信息

du7egjpx  于 2023-04-20  发布在  Oracle
关注(0)|答案(1)|浏览(193)

下面是schema:

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);
   
CREATE OR REPLACE TYPE component_ty_nt AS TABLE OF REF component_ty;

CREATE TABLE Component OF component_ty(
Code PRIMARY KEY, 
Component_Description NOT NULL,
Component_Type NOT NULL,
CONSTRAINT component_type__chk
CHECK (component_type IN ('body', 'Body', 'engine', 'Engine', 'equipment', 'Equipment'))
);

CREATE TABLE MODEL (
model_name VARCHAR(20),
model_year NUMBER,
consumption VARCHAR(6) NOT NULL,
Component component_ty_nt,
Distributor distributor_ty_nt,
Car car_ty_nt,
CONSTRAINT t_pk PRIMARY KEY (Model_Name, Model_Year))
NESTED TABLE Component STORE AS component_ty_nt_TAB
NESTED TABLE Distributor STORE AS distributor_ty_nt_TAB
NESTED TABLE Car STORE AS car_ty_nt_TAB

如何检索external_component中external_company值为null的模型的信息?

guykilcj

guykilcj1#

您可以使用以下命令检查嵌套表中的值是否具有正确的类型,并且具有NULL值:

SELECT m.model_name,
       m.model_year,
       c.code
FROM   model m
       CROSS JOIN LATERAL (
         SELECT DEREF(c.COLUMN_VALUE).code AS code
         FROM   TABLE(m.component) c
         WHERE  DEREF(c.COLUMN_VALUE) IS OF (External_Component_ty)
         AND    TREAT(DEREF(c.COLUMN_VALUE) AS External_Component_ty).External_Company IS NULL
       ) c

举个最小的例子:

CREATE OR REPLACE TYPE Component_ty AS OBJECT (
  Code VARCHAR(10)
) NOT FINAL;

CREATE OR REPLACE TYPE External_Component_ty UNDER Component_ty(
  External_Company NUMBER
);

CREATE OR REPLACE TYPE component_ty_nt AS TABLE OF REF component_ty;

CREATE TABLE Component OF component_ty;

CREATE TABLE MODEL (
  model_name VARCHAR(20),
  model_year NUMBER,
  Component component_ty_nt,
  CONSTRAINT t_pk PRIMARY KEY (Model_Name, Model_Year)
)
NESTED TABLE Component STORE AS component_ty_nt_TAB

INSERT INTO component
SELECT external_component_ty('A', 1) FROM DUAL UNION ALL
SELECT external_component_ty('B', NULL) FROM DUAL UNION ALL
SELECT component_ty('C') FROM DUAL;

INSERT INTO model
SELECT 'A', 1, component_ty_nt((SELECT REF(c) FROM component c WHERE code = 'A')) FROM DUAL UNION ALL
SELECT 'B', 1, component_ty_nt((SELECT REF(c) FROM component c WHERE code = 'B')) FROM DUAL UNION ALL
SELECT 'C', 1, component_ty_nt((SELECT REF(c) FROM component c WHERE code = 'C')) FROM DUAL UNION ALL
SELECT 'D', 1, component_ty_nt(
  (SELECT REF(c) FROM component c WHERE code = 'A'),
  (SELECT REF(c) FROM component c WHERE code = 'C')
) FROM DUAL UNION ALL
SELECT 'E', 1, component_ty_nt(
  (SELECT REF(c) FROM component c WHERE code = 'B'),
  (SELECT REF(c) FROM component c WHERE code = 'C')
) FROM DUAL UNION ALL
SELECT 'F', 1, component_ty_nt(
  (SELECT REF(c) FROM component c WHERE code = 'A'),
  (SELECT REF(c) FROM component c WHERE code = 'B'),
  (SELECT REF(c) FROM component c WHERE code = 'C')
) FROM DUAL;

输出:
| 型号名称|型号_年份|代码|
| --------------|--------------|--------------|
| B|1|B|
| E|1|B|
| F|1|B|
fiddle

相关问题