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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10318272/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 04:08:52  来源:igfitidea点击:

How to SELECT from object type column in Oracle 11g?

oracleobjectselectoracle11g

提问by snijele

Hy guys, I have following two Oracle objects:

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

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

And also, there is another object:

而且,还有另一个对象:

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

There is also following table:

还有下表:

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

Here is some data:

这是一些数据:

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

Finally, my question is: How to select only number of doors and number of seats from vehicle table column?

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

I tried following but it does not work:

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

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

I got following error:

我收到以下错误:

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

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

我唯一可以在没有任何错误的情况下获得的参数是来自汽车对象的参数。

FYI, I am using Oracle 11g on CentOS 6.2

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

Cheers, Bojan

干杯,博扬

回答by Bob Jarvis - Reinstate Monica

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

您需要使用 TREAT 函数让数据库引擎将 VEHICLE 视为 TRUCK,如下所示:

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

Share and enjoy.

分享和享受。