简体   繁体   中英

How can I get the data types of all fields from the sql query result. I am using oracle DB

I am using following query

SELECT T1.ID, 
       T1.MID,
       T2.VID
       case when T1.MID = 0 then 'FALSE' else 'TRUE' end AS ISACTIVE
FROM Table1 T1 LEFT JOIN Table2 T2 ON T1.MID = T2.MID  

I want to know the data type of the field "ISACTIVE" . I am using oracle SQL Developer. Is there any way to describe the query result?

In this case it will be varchar2(5) because the two literal values are different lengths.

The only way I know of is to create an object and test it.

SQL> create table tst as
  2   (select case when dummy='X' then 'FALSE' else 'TRUE' end  col1
  3    from dual);

Table created.

SQL> desc tst
 Name                                      Null?    Type
 ----------------------------------------- -------- ---------------------

 COL1                                               VARCHAR2(5)

SQL> drop table tst;

Table dropped.

SQL> create table tst as
  2   (select case when dummy='X' then 'FALSE' else 'TRUEy' end  col1
  3    from dual);

Table created.

SQL> desc tst
 Name                                      Null?    Type
 ----------------------------------------- -------- ---------------------

 COL1                                               CHAR(5)

SQL>

Here is a rough draft of an anonymous block that uses the DBMS_SQL package to get the datatype.

The decode statement is based on the all_tab_cols view:

select text from all_views where view_name = 'ALL_TAB_COLS';

set serveroutput on;
declare
  cursor_num integer;
  ret_num integer;
  num_cols integer;
  type_tab dbms_sql.desc_tab2;
  data_typ varchar2(512 char);
  l_query clob;
begin
  l_query := 'select case when dummy=''x'' then ''true'' else ''false'' end, 1.2, date''2015-01-01''  from dual';

  cursor_num := dbms_sql.open_cursor;

  dbms_sql.parse(cursor_num,l_query,dbms_sql.native);
  dbms_sql.describe_columns2(cursor_num,num_cols,type_tab);

  for i in 1..num_cols
  loop
    select  decode(type_tab(i).col_type, 1, decode(type_tab(i).col_charsetform, 2, 'NVARCHAR2', 'VARCHAR2'),
                       2, decode(type_tab(i).col_scale , null,
                                 decode(type_tab(i).col_precision, null, 'NUMBER', 'FLOAT'),
                                 'NUMBER'),
                       8, 'LONG',
                       9, decode(type_tab(i).col_charsetform, 2, 'NCHAR VARYING', 'VARCHAR'),
                       12, 'DATE',
                       23, 'RAW', 24, 'LONG RAW',
                       69, 'ROWID',
                       96, decode(type_tab(i).col_charsetform, 2, 'NCHAR', 'CHAR'),
                       100, 'BINARY_FLOAT',
                       101, 'BINARY_DOUBLE',
                       105, 'MLSLABEL',
                       106, 'MLSLABEL',
                       112, decode(type_tab(i).col_charsetform, 2, 'NCLOB', 'CLOB'),
                       113, 'BLOB', 114, 'BFILE', 115, 'CFILE',
                       178, 'TIME(' ||type_tab(i).col_scale|| ')',
                       179, 'TIME(' ||type_tab(i).col_scale|| ')' || ' WITH TIME ZONE',
                       180, 'TIMESTAMP(' ||type_tab(i).col_scale|| ')',
                       181, 'TIMESTAMP(' ||type_tab(i).col_scale|| ')' || ' WITH TIME ZONE',
                       231, 'TIMESTAMP(' ||type_tab(i).col_scale|| ')' || ' WITH LOCAL TIME ZONE',
                       182, 'INTERVAL YEAR(' ||type_tab(i).col_precision||') TO MONTH',
                       183, 'INTERVAL DAY(' ||type_tab(i).col_precision||') TO SECOND(' ||
                             type_tab(i).col_scale || ')',
                       208, 'UROWID',
                       'UNDEFINED') 
    into data_typ from dual;

    dbms_output.put_line('Column ' || i || ' is of type ' || data_typ);
  end loop;
  dbms_sql.close_cursor(cursor_num);
end;
/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM