Get data type of field in select statement in ORACLE

后端 未结 7 1487
半阙折子戏
半阙折子戏 2020-12-25 13:04

Can I get data types of each column I selected instead of the values, using a select statement?

FOR EXAMPLE:

SELECT a.name, a.surname, b.ordernum 
FR         


        
相关标签:
7条回答
  • 2020-12-25 13:09

    I came into the same situation. As a workaround, I just created a view (If you have privileges) and described it and dropped it later. :)

    0 讨论(0)
  • 2020-12-25 13:18

    If you don't have privileges to create a view in Oracle, a "hack" around it to use MS Access :-(

    In MS Access, create a pass through query with your sql (but add where clause to just select 1 record), create a select query from the view (very important), selecting all *, then create a make table from the select query. When this runs it will create a table with one record, all the data types should "match" oracle. i.e. Passthrough --> Select --> MakeTable --> Table

    I am sure there are other better ways, but if you have limited tools and privileges this will work.

    0 讨论(0)
  • 2020-12-25 13:19

    You can query the all_tab_columns view in the database.

    SELECT  table_name, column_name, data_type, data_length FROM all_tab_columns where table_name = 'CUSTOMER'
    
    0 讨论(0)
  • 2020-12-25 13:21
    you can use the DBMS_SQL.DESCRIBE_COLUMNS2
    
        SET SERVEROUTPUT ON;
    DECLARE
        STMT CLOB;
        CUR NUMBER;
        COLCNT NUMBER;
        IDX NUMBER;
        COLDESC DBMS_SQL.DESC_TAB2;
    BEGIN
        CUR := DBMS_SQL.OPEN_CURSOR;
        STMT := 'SELECT  object_name , to_char(object_id), created FROM    DBA_OBJECTS where rownum<10';
    
        SYS.DBMS_SQL.PARSE(CUR, STMT, DBMS_SQL.NATIVE);
        DBMS_SQL.DESCRIBE_COLUMNS2(CUR, COLCNT, COLDESC);
        DBMS_OUTPUT.PUT_LINE('Statement: ' || STMT);
        FOR IDX IN 1 .. COLCNT
        LOOP
            CASE COLDESC(IDX).col_type
            WHEN 2 THEN
                DBMS_OUTPUT.PUT_LINE('#' || TO_CHAR(IDX) || ': NUMBER');
            WHEN 12 THEN
                DBMS_OUTPUT.PUT_LINE('#' || TO_CHAR(IDX) || ': DATE');
            WHEN 180 THEN
                DBMS_OUTPUT.PUT_LINE('#' || TO_CHAR(IDX) || ': TIMESTAMP');
            WHEN 1 THEN
                DBMS_OUTPUT.PUT_LINE('#' || TO_CHAR(IDX) || ': VARCHAR'||':'|| COLDESC(IDX).col_max_len);
            WHEN 9 THEN
                DBMS_OUTPUT.PUT_LINE('#' || TO_CHAR(IDX) || ': VARCHAR2');
            -- Insert more cases if you need them
            ELSE
                DBMS_OUTPUT.PUT_LINE('#' || TO_CHAR(IDX) || ': OTHERS (' || TO_CHAR(COLDESC(IDX).col_type) || ')');
            END CASE;
        END LOOP;
        SYS.DBMS_SQL.CLOSE_CURSOR(CUR);
    EXCEPTION 
        WHEN OTHERS THEN
            DBMS_OUTPUT.PUT_LINE(SQLERRM(SQLCODE()) || ': ' || DBMS_UTILITY.FORMAT_ERROR_BACKTRACE);
            SYS.DBMS_SQL.CLOSE_CURSOR(CUR);
    END;
    /
    
    
    full example in the below url
    
    https://www.ibm.com/support/knowledgecenter/sk/SSEPGG_9.7.0/com.ibm.db2.luw.sql.rtn.doc/doc/r0055146.html
    
    0 讨论(0)
  • 2020-12-25 13:31

    I usually create a view and use the DESC command:

    CREATE VIEW tmp_view AS 
    SELECT 
          a.name
        , a.surname
        , b.ordernum 
    FROM customer a
      JOIN orders b
        ON a.id = b.id
    

    Then, the DESC command will show the type of each field.

    DESC tmp_view

    0 讨论(0)
  • 2020-12-25 13:32

    I found a not-very-intuitive way to do this by using DUMP()

    SELECT DUMP(A.NAME), 
           DUMP(A.surname), 
           DUMP(B.ordernum) 
    FROM   customer A 
           JOIN orders B 
             ON A.id = B.id
    

    It will return something like:

    'Typ=1 Len=2: 0,48' for each column.

    Type=1 means VARCHAR2/NVARCHAR2
    Type=2 means NUMBER/FLOAT
    Type=12 means DATE, etc.

    You can refer to this oracle doc for information Datatype Code
    or this for a simple mapping Oracle Type Code Mappings

    0 讨论(0)
提交回复
热议问题