如何从sql查询结果中获取所有字段的数据类型。我正在使用oracle DB

时间:2022-09-06 17:03:29

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?

我想知道“ISACTIVE”字段的数据类型。我正在使用oracle SQL Developer。有没有办法描述查询结果?

1 个解决方案

#1


0  

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

在这种情况下,它将是varchar2(5),因为两个文字值是不同的长度。

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.

下面是一个匿名块的草稿,它使用DBMS_SQL包来获取数据类型。

The decode statement is based on the all_tab_cols view:

decode语句基于all_tab_cols视图:

select text from all_views where view_name = 'ALL_TAB_COLS';

从all_views中选择文本,其中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;
/

#1


0  

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

在这种情况下,它将是varchar2(5),因为两个文字值是不同的长度。

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.

下面是一个匿名块的草稿,它使用DBMS_SQL包来获取数据类型。

The decode statement is based on the all_tab_cols view:

decode语句基于all_tab_cols视图:

select text from all_views where view_name = 'ALL_TAB_COLS';

从all_views中选择文本,其中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;
/