如何在SQL中获取对象类型的属性?

时间:2018-04-25 13:29:53

标签: sql oracle

我创建了一个对象类型和一个包含该类型列的表:

CREATE TYPE food_t AS OBJECT (
   NAME         VARCHAR2 (100)
 , food_group   VARCHAR2 (100)
 , grown_in     VARCHAR2 (100)
)
/

CREATE TABLE food_table (id number primary key, my_food food_t)
/

BEGIN
   INSERT INTO food_table
        VALUES (1, NEW food_t ('Mutter Paneer', 'Curry', 'India'));

   INSERT INTO food_table
        VALUES (2, NEW food_t ('Cantaloupe', 'Fruit', 'Backyard'));

   COMMIT;
END;
/

但是当我试图获得我的好名字时,我得到了ORA-904错误:

SELECT name FROM food_table;
ORA-00904: "NAME": invalid identifier

如果我使用对象类型列的名称,则相同:

SELECT my_food.name FROM food_table;
ORA-00904: "NAME": invalid identifier

我做错了什么?

1 个答案:

答案 0 :(得分:2)

简单:不要忘记包含表别名。在这种情况下需要:

SQL> SELECT ft.my_food.name FROM food_table ft;

MY_FOOD.NAME                                                                    
-----------------------------------------------
Mutter Paneer                                                                   
Cantaloupe                                                                      

SQL> SELECT ft.my_food.name FROM food_table ft
  2   WHERE ft.my_food.name LIKE 'C%';

MY_FOOD.NAME                                                                    
-----------------------------------------------
Cantaloupe   

如果您正在使用Oracle中的对象类型,请务必查看Object-Relational Developer's Guide