vertical display of table [message #463142] |
Tue, 29 June 2010 15:50 |
geneeyuss
Messages: 53 Registered: June 2010
|
Member |
|
|
I would like to format my output for the select query.
Example:
SQL>select empno, ename from emp;
empno ename
1987766 Jack Blake
187765 Greg Ni
...
...
...
But I would like to get the output in the below format
EMPNO:.................1987766
ENAME:.................Jack Blake
EMPNO:.................187765
ENAME:.................Greg Ni
I will need help in printing the column name. I would like to know how do we address the column name to print.
Thanks
geneeyuss
|
|
|
|
Re: formatting output for select stmt [message #463175 is a reply to message #463157] |
Wed, 30 June 2010 00:36 |
Its_me_ved
Messages: 979 Registered: October 2009 Location: India
|
Senior Member |
|
|
@Blackswan,
It even does not work for me either!!
SQL> ed
Wrote file afiedt.buf
1 select 'EMPNO..........'||empno empid,
2 'ENAME..........'||ename ename
3* from emp where rownum<3
SQL> /
EMPID ENAME
-------------------------------------------------- --------------------
EMPNO..........7369 ENAME..........SMITH
EMPNO..........7499 ENAME..........ALLEN
Could be?
SQL> ed
Wrote file afiedt.buf
1 select 'EMPNO..........'||empno ||chr(10)||
2 'ENAME..........'||ename output
3* from emp --where rownum<3
SQL> /
OUTPUT
---------------------
EMPNO..........7369
ENAME..........SMITH
EMPNO..........7499
ENAME..........ALLEN
@OP, Search for pivot here in this forum
Regards
Ved
[Updated on: Wed, 30 June 2010 00:49] Report message to a moderator
|
|
|
Re: formatting output for select stmt [message #463178 is a reply to message #463175] |
Wed, 30 June 2010 01:04 |
Its_me_ved
Messages: 979 Registered: October 2009 Location: India
|
Senior Member |
|
|
SQL> select rpad('EMPNO:',23,'.')||empno ||chr(10)||
2 rpad('ENAME:',23,'.')||ename output
3 from emp where rownum<3;
OUTPUT
----------------------------
EMPNO:.................7369
ENAME:.................SMITH
EMPNO:.................7499
ENAME:.................ALLEN
Regards
Ved
|
|
|
|
|
vertical display of table [message #463285 is a reply to message #463142] |
Wed, 30 June 2010 09:44 |
geneeyuss
Messages: 53 Registered: June 2010
|
Member |
|
|
vertical display of table
Example:
SQL> select empno, ename from emp;
empno ename
12345 john Blake
65432 Michael George
I would like to get the output like below:
SQL>@vertical_output
Enter Table Name: emp
EMPNO:............12345
ENAME:............John Blake
EMPNO:............65432
ENAME:............Michael George
Could I know how do we get this. I am stuck at a point where I print the column names.
Can anyone let me know how to do this?
Thanks
geneeyuss
|
|
|
Re: vertical display of table [message #463288 is a reply to message #463285] |
Wed, 30 June 2010 09:52 |
geneeyuss
Messages: 53 Registered: June 2010
|
Member |
|
|
In your case you know the table name and the columns you wanna output. I want this to be generic for any given table. In that case I should how to print the column name.
Could someone please help me on that.
Thanks
geneeyuss
|
|
|
|
Re: vertical display of table [message #463295 is a reply to message #463292] |
Wed, 30 June 2010 10:22 |
geneeyuss
Messages: 53 Registered: June 2010
|
Member |
|
|
I tried this before also. one of my collegues gave permission to me to execute it. but doesnt seems to give me any output at all.
Example:
SQL> exec pv('select * from FNCL_AST');
PL/SQL procedure successfully completed.
Elapsed: 00:00:00.01
I just renamed the same file to pv. Could I know if someone can help me.
thanks
geneeyuss
|
|
|
|
Re: vertical display of table [message #463299 is a reply to message #463142] |
Wed, 30 June 2010 10:28 |
geneeyuss
Messages: 53 Registered: June 2010
|
Member |
|
|
SQL> set serveroutput on
SQL> exec pv('select * from FNCL_AST');
PL/SQL procedure successfully completed.
Elapsed: 00:00:00.01
pv.sql
create or replace procedure print_table( p_query in varchar2 )
AUTHID CURRENT_USER
is
l_theCursor integer default dbms_sql.open_cursor;
l_columnValue varchar2(4000);
l_status integer;
l_descTbl dbms_sql.desc_tab;
l_colCnt number;
begin
execute immediate
'alter session set
nls_date_format=''dd-mon-yyyy hh24:mi:ss'' ';
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 );
dbms_output.put_line
( rpad( l_descTbl(i).col_name, 30 )
|| ': ' ||
l_columnValue );
end loop;
dbms_output.put_line( '-----------------' );
end loop;
execute immediate
'alter session set nls_date_format=''dd-MON-rr'' ';
exception
when others then
execute immediate
'alter session set nls_date_format=''dd-MON-rr'' ';
raise;
end;
/
|
|
|
|
|
|
Re: vertical display of table [message #463322 is a reply to message #463321] |
Wed, 30 June 2010 14:27 |
geneeyuss
Messages: 53 Registered: June 2010
|
Member |
|
|
I tried keeping the name of the procedure print_table and it worked for me. but if I change the procedure name to pv and exec pv I dont get any output. but I dot get it with print_table as the procedure name.
I have no idea y this is happening?
|
|
|
Re: vertical display of table [message #463323 is a reply to message #463322] |
Wed, 30 June 2010 14:34 |
geneeyuss
Messages: 53 Registered: June 2010
|
Member |
|
|
i just tried it works for me if the procedure name is print_table and doesnt work if I change the procedure name to pv
SQL> exec print_table('select * from im2.emp where rownum <3');
EMPNO : 100775146
ENAME : John Blake
HIRE_DT : 01-jun-2009 16:36:10
SALARY : 90000
-----------------
EMPNO : 100800985
ENAME : Michael George
HIRE_DT : 30-may-2009 18:52:52
SALARY : 80000
-----------------
PL/SQL procedure successfully completed.
Elapsed: 00:00:00.01
if I change the procedure name to pv I cannot run it.
SQL> exec pv('select * from im2.emp where rownum <3')
PL/SQL procedure successfully completed.
Elapsed: 00:00:00.00
there is no difference other than me change the name of the procedure from print_table to pv.
it works fine with print_table. but just curious why it ditn work with procedure name pv.
Thanks
geneeyuss
|
|
|
Re: vertical display of table [message #463325 is a reply to message #463323] |
Wed, 30 June 2010 14:50 |
geneeyuss
Messages: 53 Registered: June 2010
|
Member |
|
|
This is what I have with that command:
SQL> SELECT OBJECT_TYPE, OWNER from DBA_OBJECTS WHERE OBJECT_NAME = 'PV';
OBJECT_TYPE OWNER
------------------- ------------------------------
PROCEDURE B8345B
SYNONYM Im2_TOOL
SYNONYM Im2
SYNONYM B22222 ----- thats me
Elapsed: 00:00:00.06
|
|
|
|
Re: vertical display of table [message #463327 is a reply to message #463326] |
Wed, 30 June 2010 15:32 |
geneeyuss
Messages: 53 Registered: June 2010
|
Member |
|
|
I think the mistake was I forgot that my colleague had granted select privilege on a function pv which didn't work. Now when I try to create a procedure that's where it strying to look per my understanding.
Thanks for troubleshooting my problem.
|
|
|