Rameshwar.S.Soni Rameshwar.S.Soni - 1 month ago 10
SQL Question

Select query error in Oracle

I first created a Type and then created a Table and then inserted 1 row into the table which was successful but when I fire the Select query on Table I get errors:


create type My_type as Object
(
fname varchar2(10),
lname varchar2(10)
);

Now created the table

create table My_table
(
name My_type,
phone number
);

Inserted a row

insert into My_table values ( My_type('Abc','Xyz'), 122);

Fire the Select query

select * from My_table;

Error : ORA-00932: inconsistent datatypes: expected NUMBER got OODB.MY_TYPE

select name.fname, name.lname, phone from My_table;

Error : ORA-00904: "NAME"."LNAME": invalid identifier

select My_type.fname, My_type.lname, phone from My_table;
Error : ORA-00904: "MY_TYPE"."LNAME": invalid identifier

Answer

If you use a current version of SQL*Plus, this should work

SQL> create type My_type as Object ( fname varchar2(10), lname varchar2(10) );
  2  /

Type created.

SQL> create table My_table ( name My_type, phone number );

Table created.

SQL> insert into My_table values ( My_type('Abc','Xyz'), 122);

1 row created.

SQL> column name format a30;
SQL> select * from My_table;

NAME(FNAME, LNAME)                  PHONE
------------------------------ ----------
MY_TYPE('Abc', 'Xyz')                 122

It is possible that if you're using a really old version of SQL*Plus or if you're using some other client tool that the client tool doesn't know how to handle object types.

Outside of classroom exercises, you don't generally define tables that have object types in the real world. It's much more common to use object types in PL/SQL code to simplify processing-- dealing with objects that are stored as part of the data model, on the other hand, tends to be problematic.