如何从Oracle 11g的对象类型列中进行选择? [英] How to SELECT from object type column in Oracle 11g?

查看:47
本文介绍了如何从Oracle 11g的对象类型列中进行选择?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下两个Oracle对象:

Hy guys, I have following two Oracle objects:

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 
);

还有下表:

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 ) );

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

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;

我遇到以下错误:

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

分享并享受.

这篇关于如何从Oracle 11g的对象类型列中进行选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆