Listing SQL Output

I have a table emp with following structure

Empid Varchar2(20)
empname Varchar2(30)
empstreet Varchar2(30)
...

How can I write a query that will return the output

Record 1
Empid : elkfelkfe
empname : sdhdsdjskj
empstreet: ds8w7d8w7d
...
....
empzip : 49384


Record 2
Empid : elkfelkfe
empname : sdhdsdjskj
empstreet: ds8w7d8w7d
...
....
empzip : 49384


Record 3
Empid : elkfelkfe
empname : sdhdsdjskj
empstreet: ds8w7d8w7d
...
....
empzip : 49384


.......

Thank You

Shariq
shariqAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jwittenmCommented:
I'm not sure how to get the "Record N" heading, but following is a way to get the output in the form you want it using pipelined functions.  Courtesy of Tom Kyte from asktom.oracle, with a slight modification.

> create or replace type myScalarType as object
  (cname varchar2(30), val varchar2(4000));
  /

> create or replace type myTableType as table of myScalarType;
 /

create or replace function
cols_as_rows(p_query in varchar2) return myTableType
authid current_user
pipelined
as
      l_theCursor            integer default dbms_sql.open_cursor;
      l_columnValue      varchar2(4000);
      l_status            integer;
      l_colCnt            number default 0;
      l_descTbl            dbms_sql.desc_tab;
      l_rnum            number := 1;
begin
      dbms_sql.parse(l_theCursor, p_query, dbms_sql.native);
      dbms_sql.describe_columns(l_theCursor, l_colCnt, l_descTbl);
      for i in 1 .. l_colCnt loop
            dbms_sql.define_column(l_theCursor, i, l_columnValue, 4000);
      end loop;
      l_status := dbms_sql.execute(l_theCursor);
      while (dbms_sql.fetch_rows(l_theCursor) > 0 ) loop
            for i in 1 .. l_colCnt loop
                  dbms_sql.column_value(l_theCursor, i, l_columnValue);
                  pipe row
                        (myScalarType(l_descTbl(i).col_name, l_columnValue));
            end loop;
            l_rnum := l_rnum + 1;
      end loop;
      dbms_sql.close_cursor(l_theCursor);
      return;
end cols_as_rows;
/

select * from table(cols_as_rows('select * from <table>'));
DrSQLCommented:
shariq,
   That's almost an xml format.  I'd write a short pl/sql block and run it in sql*plus:

set serveroutput on

begin
  declare
     which number := 0;
begin
   for this in (select * from emp) loop
      which := which+1;
      dbms_output.put_line('Record '||to_char(which));
      dbms_output.put_line('Empid: '|| this.empid);
      dbms_output.put_line('Empname: '|| this.empname);
     <etc.>
   end loop;
end;
end;
/


Good luck!
DrSQL
 
NarcisFCommented:
select 'Record' || rownum || char(10) ||
          'Empid: '|| empid || char(10) ||
          'Empname: '|| empname
          .....
 from emp;


I hope this is what you needed.

Regards,
Narcis

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Your Guide to Achieving IT Business Success

The IT Service Excellence Tool Kit has best practices to keep your clients happy and business booming. Inside, you’ll find everything you need to increase client satisfaction and retention, become more competitive, and increase your overall success.

GiggsipCommented:
I like NarcisF's idea, but just a little correction:
It's CHR and not CHAR,
and I'm not sure if you  rather have CHR(10) or CHR(13) and maybe there will be no difference at all.
DrSQLCommented:
shariq,
   If you do use the CR/LF solution (from NarcisF and Giggsip), you'll want to have:

set head off
set pagesize 0

before you run it.

Good luck!
DrSQL
DrSQLCommented:
Henka,
   Unless shariq has other ideas, I'd suggest a split between NarcisF and Giggsip.

Good luck!
DrSQL
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Oracle Database

From novice to tech pro — start learning today.