oracle从select语句中读取列名

时间:2016-12-14 07:34:13

标签: oracle select names

我想知道如何在oracle中读取列名。好的,我知道有一个名为USER_TAB_COLUMNS的表,它提供了有关它的信息,但如果我有2或3级嵌套查询,我不知道列名。或者我只是使用join语句进行简单查询,我想得到列名。怎么做?任何想法?

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

感谢

2 个答案:

答案 0 :(得分:0)

我会选择:

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

从数据库中获取查询。要获取具有填充地图类型的列,您只需使用:

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

答案 1 :(得分:0)

我认为你正在寻找这个:

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;