Select to display the column name of a table?

Asked

Viewed 9,821 times

0

Tables

col1| col2| col3| ...| coln
 1  |  2  |  3  | ...|  n

Display the name :

col1
col2
col3
...
coln

2 answers

3


  • 1

    Your answer makes sense, but a reference would be welcome

1

This Function is useful for developers as it generates an editable list for SQL

create or replace FUNCTION colunas (TABELA  IN USER_TABLES.TABLE_NAME%TYPE,
                                    PREFIXO IN VARCHAR2 DEFAULT NULL,
                                    SUFIXO  IN VARCHAR2 DEFAULT NULL) RETURN VARCHAR2 IS
  VS_SAI VARCHAR2(4000);
  /*GERA LISTA COM AS COLUNAS DE UMA TABELA*/
BEGIN
  FOR R IN (SELECT COLUMN_NAME
            FROM   USER_TAB_COLUMNS
            WHERE  TABLE_NAME = TABELA
            ORDER BY COLUMN_ID)
  LOOP
    VS_SAI := VS_SAI || PREFIXO || R.COLUMN_NAME || SUFIXO ||',';
  END LOOP;
  VS_SAI := SUBSTR(VS_SAI,1,LENGTH(VS_SAI)-1);
  RETURN VS_SAI;
END;

Do :

select columns('') from dual

Browser other questions tagged

You are not signed in. Login or sign up in order to post.