2016-02-11 32 views

回答

0

在這種情況下,它將是varchar2(5),因爲兩個文字值的長度不同。

我知道的唯一方法是創建一個對象並對其進行測試。

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> 

這是一個使用DBMS_SQL包獲取數據類型的匿名塊的粗略草案。

譯碼語句是基於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; 
/
+0

謝謝Andy的迴應。我沒有權限創建表格 – Chinnu

+0

有沒有其他方法? – Chinnu

相關問題