oracle read column names from select statement

青春壹個敷衍的年華 提交于 2019-12-12 04:32:46

问题


I wonder how read column names in oracle. Ok, I know that there is table named USER_TAB_COLUMNS which gives info about it, but if I have 2 or 3 level nested query and I don't know column names. Or I just have simple query with join statement and i want to get column names. How to do that? any idey?

select * from person a
join person_details b where a.person_id = b.person_id

thanks


回答1:


I would go for:

select 'select ' || LISTAGG(column_name , ',') within group (order by column_id) || ' from T1' 
  from user_tab_columns 
  where table_name = 'T1';

to get a query from database. To get columns with types to fill map you can use just:

select column_name , data_type
      from user_tab_columns 
      where table_name = 'T1';



回答2:


I assume you are looking for this:

DECLARE

    sqlStr VARCHAR2(1000);
    cur INTEGER;
    columnCount INTEGER;
    describeColumns DBMS_SQL.DESC_TAB2;

BEGIN
    sqlStr := 'SELECT a.*, b.*, SYSDATE as "Customized column name" 
              FROM person a JOIN person_details b 
              WHERE a.person_id = b.person_id';

    cur := DBMS_SQL.OPEN_CURSOR;
    DBMS_SQL.PARSE(cur, sqlStr, DBMS_SQL.NATIVE);
    DBMS_SQL.DESCRIBE_COLUMNS2(cur, columnCount, describeColumns);      
    FOR i IN 1..columnCount LOOP
        DBMS_OUTPUT.PUT_LINE ( describeColumns(i).COL_NAME );
    END LOOP;
    DBMS_SQL.CLOSE_CURSOR(cur);

END;    


来源:https://stackoverflow.com/questions/41137102/oracle-read-column-names-from-select-statement

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!