How to converts rows into column in a table

Recently I encountered a oracle table which has 50 column and I need to analyze the each column for the particular row. It was tough viewing those 50 columns in the rows, So I created below PLSQL block to convert the rows into column. This takes table name as the input and print one rows in column format

set serveroutput on
colname varchar2(100);
sql_str VARCHAR2(200);
col_value varchar(100);
--------- !!!! Carefully change this select state ment !!! --------------
cursor cur2 is select COLUMN_NAME from dba_tab_columns where TABLE_NAME='&1';
for rec1 in cur2
colname :=rec1.COLUMN_NAME;
sql_str:='select '|| colname ||' from apps.&&1 where rownum< 2';
EXECUTE IMMEDIATE sql_str into col_value;
dbms_output.put_line ( colname ||':'||col_value );
end loop;

Leave a Reply