- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Horizontal Output from ORACLE SQL
Operating System - HP-UX
1820539
Members
3785
Online
109626
Solutions
Forums
Categories
Company
Local Language
юдл
back
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Discussions
Forums
Forums
Discussions
юдл
back
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Blogs
Information
Community
Resources
Community Language
Language
Forums
Blogs
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-20-2007 05:36 AM
тАО02-20-2007 05:36 AM
Horizontal Output from ORACLE SQL
Hi,
I have around 200 columns in a table.
I'm using all_tab_columns to fetch the column names. which displays
Col1
Col2
Col3
etc...
I need the output as
Col1,Col2,Col3...how to do this ?
Thanks
I have around 200 columns in a table.
I'm using all_tab_columns to fetch the column names. which displays
Col1
Col2
Col3
etc...
I need the output as
Col1,Col2,Col3...how to do this ?
Thanks
3 REPLIES 3
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-20-2007 05:42 AM
тАО02-20-2007 05:42 AM
Re: Horizontal Output from ORACLE SQL
Imho it's possible to do this only through a stored procedure on an anonymous PL/SQL block. Don't think SQL*Plus can do this for you.
~cheers
~cheers
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-20-2007 02:43 PM
тАО02-20-2007 02:43 PM
Re: Horizontal Output from ORACLE SQL
I could solve that problem in a few lines of line of easy perl... or 50 lines of wild and wacky sql.
Perl first:
foreach (`sqlplus -s user/password \@tab_cols.sql`) {
my($table,$col) = split;
if ($table ne $old) {
if ($old) {
print "$old $cols\n";
}
$cols = $col;
$old = $table;
} else {
$cols .= "," . $col;
}
}
# Last blank line triggers last table output.
Where tab_cols.sql reads:
set pages 0 lines 9999 head off feedbac off
select table_name, column_name from user_tab_columns order by table_name, column_id;
exit
Now for SQL there is this newfangled (Oracle 10g or better !) MODEL function, which is like a PIVOT TABLE.
I found a usage example in the "SQL COOKBOOK"
Chapter 11, "Creating CVS output from Oracle"
http://www.oreilly.com/catalog/sqlckbk/
I adapted this to use USER_TAB_COLUMNS as follows:
select table_name, list "Columns..."
from (
select *
from (
select table_name, column_id, column_name,
lag (table_name) over (partition by table_name order by column_id) prior_table
from user_tab_columns
)
model
dimension by (
table_name, row_number() over(partition by table_name order by column_id) rn
)
measures (
column_name, prior_table, cast(null as varchar2(500)) list,
count(*) over (partition by table_name) cnt,
row_number() over(partition by table_name order by column_id) rnk
)
rules (
list[any,any]
order by table_name, rn =
case
when prior_table[cv(),cv()] is null
then column_name[cv(),cv()]
else column_name[cv(),cv()] || ',' || list[cv(),rnk[cv(),cv()]-1]
end
)
)
where cnt = rn;
There is no way I could have made that up!
I just modified the template.
The result for a test account on a 10g XE install on my PC is an (amazing!)...
SQL> @csv.sql
TABLE_NAME Columns...
------------------------------ ----------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------------
---
BIN$UTvAkVYzQ0WadVJQMNzvGg==$0 C,B,A
MY_PLAN_TABLE FILTER_PREDICATES,ACCESS_PREDICATES,TEMP_SPACE,IO_COST,CPU_COST,DISTRIBUTION,OTHER,PARTITION_ID,PART
TION_STOP,PARTITION_START,OTHER_TAG,BYTES,CARDINALITY,COST,POSITION,PARENT_ID,ID,SEARCH_COLUMNS,OPTIMIZER,OBJECT_TYPE,OBJECT_INSTAN
E,OBJECT_NAME,OBJECT_OWNER,OBJECT_NODE,OPTIONS,OPERATION,REMARKS,TIMESTAMP,STATEMENT_ID
TEST TEST
VT_FEATURE DESCRIPTION,FEATURE
VT_FEATURE_BASE DESCRIPTION,FEATURE
x rows selected.
SQL>
For specific tables, change that "from user_tab_columns" in the middle to "from user_tab_columns where table_name like '%....%'"
Regards,
Hein van den Heuvel
HvdH Performance Consulting
Perl first:
foreach (`sqlplus -s user/password \@tab_cols.sql`) {
my($table,$col) = split;
if ($table ne $old) {
if ($old) {
print "$old $cols\n";
}
$cols = $col;
$old = $table;
} else {
$cols .= "," . $col;
}
}
# Last blank line triggers last table output.
Where tab_cols.sql reads:
set pages 0 lines 9999 head off feedbac off
select table_name, column_name from user_tab_columns order by table_name, column_id;
exit
Now for SQL there is this newfangled (Oracle 10g or better !) MODEL function, which is like a PIVOT TABLE.
I found a usage example in the "SQL COOKBOOK"
Chapter 11, "Creating CVS output from Oracle"
http://www.oreilly.com/catalog/sqlckbk/
I adapted this to use USER_TAB_COLUMNS as follows:
select table_name, list "Columns..."
from (
select *
from (
select table_name, column_id, column_name,
lag (table_name) over (partition by table_name order by column_id) prior_table
from user_tab_columns
)
model
dimension by (
table_name, row_number() over(partition by table_name order by column_id) rn
)
measures (
column_name, prior_table, cast(null as varchar2(500)) list,
count(*) over (partition by table_name) cnt,
row_number() over(partition by table_name order by column_id) rnk
)
rules (
list[any,any]
order by table_name, rn =
case
when prior_table[cv(),cv()] is null
then column_name[cv(),cv()]
else column_name[cv(),cv()] || ',' || list[cv(),rnk[cv(),cv()]-1]
end
)
)
where cnt = rn;
There is no way I could have made that up!
I just modified the template.
The result for a test account on a 10g XE install on my PC is an (amazing!)...
SQL> @csv.sql
TABLE_NAME Columns...
------------------------------ ----------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------------
---
BIN$UTvAkVYzQ0WadVJQMNzvGg==$0 C,B,A
MY_PLAN_TABLE FILTER_PREDICATES,ACCESS_PREDICATES,TEMP_SPACE,IO_COST,CPU_COST,DISTRIBUTION,OTHER,PARTITION_ID,PART
TION_STOP,PARTITION_START,OTHER_TAG,BYTES,CARDINALITY,COST,POSITION,PARENT_ID,ID,SEARCH_COLUMNS,OPTIMIZER,OBJECT_TYPE,OBJECT_INSTAN
E,OBJECT_NAME,OBJECT_OWNER,OBJECT_NODE,OPTIONS,OPERATION,REMARKS,TIMESTAMP,STATEMENT_ID
TEST TEST
VT_FEATURE DESCRIPTION,FEATURE
VT_FEATURE_BASE DESCRIPTION,FEATURE
x rows selected.
SQL>
For specific tables, change that "from user_tab_columns" in the middle to "from user_tab_columns where table_name like '%....%'"
Regards,
Hein van den Heuvel
HvdH Performance Consulting
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-20-2007 05:29 PM
тАО02-20-2007 05:29 PM
Re: Horizontal Output from ORACLE SQL
Hi,
Why dont you connect to Oracle through MS EXCEL. This will help you.
You should have system admin privileges.
Connect to ODBC.
Choose system DSN
Click on ADD button
Choose Oracle datasource
enter the required entries.
and test connection.
Go to excel
Choose Data-->Query database.
I think this will help you.
Regards
Subodh
Why dont you connect to Oracle through MS EXCEL. This will help you.
You should have system admin privileges.
Connect to ODBC.
Choose system DSN
Click on ADD button
Choose Oracle datasource
enter the required entries.
and test connection.
Go to excel
Choose Data-->Query database.
I think this will help you.
Regards
Subodh
The opinions expressed above are the personal opinions of the authors, not of Hewlett Packard Enterprise. By using this site, you accept the Terms of Use and Rules of Participation.
Company
Learn About
News and Events
Support
© Copyright 2025 Hewlett Packard Enterprise Development LP