sqloracle-database

Oracle Object : How to show all the fields in select query?


I have the following Oracle Object :

CREATE TYPE person_typ AS OBJECT (
  Id NUMBER,
  first_name     VARCHAR2(20),
  last_name      VARCHAR2(25));

And a table :

CREATE TABLE MyTable (
  contact         person_typ,
  contact_date    DATE );

I would like to make a select query to show all the fields of the Person_Typ object without specifiying their names.

When I do

select * from MyTable

The column contact is shown as [unsupported data type], I have to use instead :

select T.contact.ID, T.contact.First_name, T.contact.last_name  from MyTable T

Is there another way to show the values of the object without specifying the column names ?


Solution

  • I don't use SQL Developer, but according to this article Showing TYPE’d Column Values in SQL Developer you could use option:

    Preferences / Database / Advanced / Display Struct Value in Grid

    Also you can query user_type_attr (or all_type_attr) to obtain column names. Then copy/paste select part from output and run it or create view as proposed by @sep. Here is my test data and code block:

    insert into mytable values (person_typ(1, 'Paulina', 'Thomson'), date '2017-12-17');
    insert into mytable values (person_typ(7, 'Keanu', 'Stevens'), date '2017-12-28');
    
    declare 
      v_sql varchar2(32000);
    begin
      select listagg('T.CONTACT.'||attr_name||' '||attr_name, ', ') 
             within group (order by attr_no) 
        into v_sql
        from user_type_attrs 
        where type_name = 'PERSON_TYP';
    
      v_sql := 'SELECT '||v_sql||' FROM MYTABLE T';
      dbms_output.put_line(v_sql);
      execute immediate 'CREATE OR REPLACE VIEW VW_CONTACTS AS '||v_sql;
    end;
    
    select * from vw_contacts;
    

    Result:

        ID FIRST_NAME           LAST_NAME
    ------ -------------------- -------------------------
         1 Paulina              Thomson
         7 Keanu                Stevens