如何从 Oracle 11g 中的对象类型列中选择?

2021-12-30 00:00:00 object oracle11g select oracle

伙计们,我有以下两个 Oracle 对象:

Hy guys, I have following two Oracle objects:

CREATE OR REPLACE TYPE car AS OBJECT( 
name VARCHAR( 80 ) 
) NOT FINAL;

此外,还有一个对象:

CREATE OR REPLACE TYPE truck UNDER car ( 
doors NUMBER,
seats NUMBER 
);

还有下表:

CREATE TABLE vehicles (
id NUMBER NOT NULL,
vehicle car,
PRIMARY KEY (id)
);

这是一些数据:

INSERT INTO vehicles ( id, vehicle ) VALUES ( 1, truck( 'ford', 4, 4 ) );
INSERT INTO vehicles ( id, vehicle ) VALUES ( 2, truck( 'toyota', 4, 5 ) );

最后,我的问题是:如何从车辆表列中只选择门数和座位数?

我尝试了以下但它不起作用:

I tried following but it does not work:

SELECT v.vehicle.doors AS doors AS seats FROM vehicles v;

我收到以下错误:

ORA-00904: "V"."VEHICLE"."DOORS": invalid identifier

我可以毫无错误地获得的唯一参数是来自汽车对象的参数.

Only parameter that i can get without any error is one from car object.

仅供参考,我在 CentOS 6.2 上使用 Oracle 11g

FYI, I am using Oracle 11g on CentOS 6.2

干杯,博扬

推荐答案

需要使用TREAT函数让数据库引擎把VEHICLE当成TRUCK,如下:

You need to use the TREAT function to get the database engine to treat VEHICLE as a TRUCK, as in:

SELECT ID, TREAT(vehicle AS TRUCK).DOORS FROM VEHICLES

分享和享受.

相关文章