<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Horizontal Output from ORACLE SQL in Operating System - HP-UX</title>
    <link>https://community.hpe.com/t5/operating-system-hp-ux/horizontal-output-from-oracle-sql/m-p/3948145#M760879</link>
    <description>&lt;!--!*#--&gt;I could solve that problem in a few lines of line of easy perl... or 50 lines of wild and wacky sql.&lt;BR /&gt;&lt;BR /&gt;Perl first:&lt;BR /&gt;&lt;BR /&gt;foreach (`sqlplus -s user/password \@tab_cols.sql`) { &lt;BR /&gt;  my($table,$col) = split;&lt;BR /&gt;  if ($table ne $old) {&lt;BR /&gt;     if ($old) {&lt;BR /&gt;        print "$old $cols\n";&lt;BR /&gt;        }&lt;BR /&gt;     $cols = $col;&lt;BR /&gt;     $old = $table;&lt;BR /&gt;     } else {&lt;BR /&gt;     $cols .= "," . $col;&lt;BR /&gt;     }&lt;BR /&gt;  }&lt;BR /&gt;# Last blank line triggers last table output.&lt;BR /&gt;&lt;BR /&gt;Where tab_cols.sql reads:&lt;BR /&gt;&lt;BR /&gt;set pages 0 lines 9999 head off feedbac off&lt;BR /&gt;select table_name, column_name from user_tab_columns order by table_name, column_id;&lt;BR /&gt;exit&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Now for SQL there is this newfangled (Oracle 10g or better !) MODEL function, which is like a PIVOT TABLE.&lt;BR /&gt;I found a usage example in the "SQL COOKBOOK"&lt;BR /&gt;Chapter 11, "Creating CVS output from Oracle"&lt;BR /&gt;&lt;A href="http://www.oreilly.com/catalog/sqlckbk/" target="_blank"&gt;http://www.oreilly.com/catalog/sqlckbk/&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;I adapted this to use USER_TAB_COLUMNS as follows:&lt;BR /&gt;&lt;BR /&gt;select table_name, list "Columns..."&lt;BR /&gt;  from (&lt;BR /&gt;    select *&lt;BR /&gt;      from (&lt;BR /&gt;        select table_name, column_id, column_name,&lt;BR /&gt;          lag (table_name) over (partition by table_name order by column_id) prior_table&lt;BR /&gt;          from user_tab_columns&lt;BR /&gt;           )&lt;BR /&gt;        model&lt;BR /&gt;          dimension by (&lt;BR /&gt;            table_name, row_number() over(partition by table_name order by column_id) rn&lt;BR /&gt;            )&lt;BR /&gt;          measures (&lt;BR /&gt;            column_name, prior_table, cast(null as varchar2(500)) list,&lt;BR /&gt;            count(*) over (partition by table_name) cnt,&lt;BR /&gt;            row_number() over(partition by table_name order by column_id) rnk&lt;BR /&gt;            )&lt;BR /&gt;          rules (&lt;BR /&gt;            list[any,any]&lt;BR /&gt;            order by table_name, rn =&lt;BR /&gt;              case&lt;BR /&gt;                when prior_table[cv(),cv()] is null&lt;BR /&gt;                  then column_name[cv(),cv()]&lt;BR /&gt;                  else column_name[cv(),cv()] || ',' || list[cv(),rnk[cv(),cv()]-1]&lt;BR /&gt;                end&lt;BR /&gt;            )&lt;BR /&gt;         )&lt;BR /&gt;  where cnt = rn;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;There is no way I could have made that up!&lt;BR /&gt;I just modified the template.&lt;BR /&gt;&lt;BR /&gt;The result for a test account on a 10g XE install on my PC is an (amazing!)...&lt;BR /&gt;&lt;BR /&gt;SQL&amp;gt; @csv.sql&lt;BR /&gt;&lt;BR /&gt;TABLE_NAME                     Columns...&lt;BR /&gt;------------------------------ ----------------------------------------------------------------------------------------------------&lt;BR /&gt;-----------------------------------------------------------------------------------------------------------------------------------&lt;BR /&gt;-----------------------------------------------------------------------------------------------------------------------------------&lt;BR /&gt;-----------------------------------------------------------------------------------------------------------------------------------&lt;BR /&gt;---&lt;BR /&gt;BIN$UTvAkVYzQ0WadVJQMNzvGg==$0 C,B,A&lt;BR /&gt;MY_PLAN_TABLE                  FILTER_PREDICATES,ACCESS_PREDICATES,TEMP_SPACE,IO_COST,CPU_COST,DISTRIBUTION,OTHER,PARTITION_ID,PART&lt;BR /&gt;TION_STOP,PARTITION_START,OTHER_TAG,BYTES,CARDINALITY,COST,POSITION,PARENT_ID,ID,SEARCH_COLUMNS,OPTIMIZER,OBJECT_TYPE,OBJECT_INSTAN&lt;BR /&gt;E,OBJECT_NAME,OBJECT_OWNER,OBJECT_NODE,OPTIONS,OPERATION,REMARKS,TIMESTAMP,STATEMENT_ID&lt;BR /&gt;TEST                           TEST&lt;BR /&gt;VT_FEATURE                     DESCRIPTION,FEATURE&lt;BR /&gt;VT_FEATURE_BASE                DESCRIPTION,FEATURE&lt;BR /&gt;&lt;BR /&gt;x rows selected.&lt;BR /&gt;&lt;BR /&gt;SQL&amp;gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;For specific tables, change that "from user_tab_columns" in the middle to "from user_tab_columns where table_name like '%....%'"&lt;BR /&gt;&lt;BR /&gt;Regards,&lt;BR /&gt;Hein van den Heuvel&lt;BR /&gt;HvdH Performance Consulting&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;</description>
    <pubDate>Tue, 20 Feb 2007 22:43:09 GMT</pubDate>
    <dc:creator>Hein van den Heuvel</dc:creator>
    <dc:date>2007-02-20T22:43:09Z</dc:date>
    <item>
      <title>Horizontal Output from ORACLE SQL</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/horizontal-output-from-oracle-sql/m-p/3948143#M760877</link>
      <description>Hi,&lt;BR /&gt;&lt;BR /&gt;I have around 200 columns in a table.&lt;BR /&gt;I'm using all_tab_columns to fetch the column names. which displays &lt;BR /&gt;Col1&lt;BR /&gt;Col2&lt;BR /&gt;Col3&lt;BR /&gt;etc...&lt;BR /&gt;&lt;BR /&gt;I need the output as &lt;BR /&gt;Col1,Col2,Col3...how to do this ? &lt;BR /&gt;&lt;BR /&gt;Thanks</description>
      <pubDate>Tue, 20 Feb 2007 13:36:07 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/horizontal-output-from-oracle-sql/m-p/3948143#M760877</guid>
      <dc:creator>uform</dc:creator>
      <dc:date>2007-02-20T13:36:07Z</dc:date>
    </item>
    <item>
      <title>Re: Horizontal Output from ORACLE SQL</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/horizontal-output-from-oracle-sql/m-p/3948144#M760878</link>
      <description>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.&lt;BR /&gt;&lt;BR /&gt;~cheers</description>
      <pubDate>Tue, 20 Feb 2007 13:42:45 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/horizontal-output-from-oracle-sql/m-p/3948144#M760878</guid>
      <dc:creator>Sandman!</dc:creator>
      <dc:date>2007-02-20T13:42:45Z</dc:date>
    </item>
    <item>
      <title>Re: Horizontal Output from ORACLE SQL</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/horizontal-output-from-oracle-sql/m-p/3948145#M760879</link>
      <description>&lt;!--!*#--&gt;I could solve that problem in a few lines of line of easy perl... or 50 lines of wild and wacky sql.&lt;BR /&gt;&lt;BR /&gt;Perl first:&lt;BR /&gt;&lt;BR /&gt;foreach (`sqlplus -s user/password \@tab_cols.sql`) { &lt;BR /&gt;  my($table,$col) = split;&lt;BR /&gt;  if ($table ne $old) {&lt;BR /&gt;     if ($old) {&lt;BR /&gt;        print "$old $cols\n";&lt;BR /&gt;        }&lt;BR /&gt;     $cols = $col;&lt;BR /&gt;     $old = $table;&lt;BR /&gt;     } else {&lt;BR /&gt;     $cols .= "," . $col;&lt;BR /&gt;     }&lt;BR /&gt;  }&lt;BR /&gt;# Last blank line triggers last table output.&lt;BR /&gt;&lt;BR /&gt;Where tab_cols.sql reads:&lt;BR /&gt;&lt;BR /&gt;set pages 0 lines 9999 head off feedbac off&lt;BR /&gt;select table_name, column_name from user_tab_columns order by table_name, column_id;&lt;BR /&gt;exit&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Now for SQL there is this newfangled (Oracle 10g or better !) MODEL function, which is like a PIVOT TABLE.&lt;BR /&gt;I found a usage example in the "SQL COOKBOOK"&lt;BR /&gt;Chapter 11, "Creating CVS output from Oracle"&lt;BR /&gt;&lt;A href="http://www.oreilly.com/catalog/sqlckbk/" target="_blank"&gt;http://www.oreilly.com/catalog/sqlckbk/&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;I adapted this to use USER_TAB_COLUMNS as follows:&lt;BR /&gt;&lt;BR /&gt;select table_name, list "Columns..."&lt;BR /&gt;  from (&lt;BR /&gt;    select *&lt;BR /&gt;      from (&lt;BR /&gt;        select table_name, column_id, column_name,&lt;BR /&gt;          lag (table_name) over (partition by table_name order by column_id) prior_table&lt;BR /&gt;          from user_tab_columns&lt;BR /&gt;           )&lt;BR /&gt;        model&lt;BR /&gt;          dimension by (&lt;BR /&gt;            table_name, row_number() over(partition by table_name order by column_id) rn&lt;BR /&gt;            )&lt;BR /&gt;          measures (&lt;BR /&gt;            column_name, prior_table, cast(null as varchar2(500)) list,&lt;BR /&gt;            count(*) over (partition by table_name) cnt,&lt;BR /&gt;            row_number() over(partition by table_name order by column_id) rnk&lt;BR /&gt;            )&lt;BR /&gt;          rules (&lt;BR /&gt;            list[any,any]&lt;BR /&gt;            order by table_name, rn =&lt;BR /&gt;              case&lt;BR /&gt;                when prior_table[cv(),cv()] is null&lt;BR /&gt;                  then column_name[cv(),cv()]&lt;BR /&gt;                  else column_name[cv(),cv()] || ',' || list[cv(),rnk[cv(),cv()]-1]&lt;BR /&gt;                end&lt;BR /&gt;            )&lt;BR /&gt;         )&lt;BR /&gt;  where cnt = rn;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;There is no way I could have made that up!&lt;BR /&gt;I just modified the template.&lt;BR /&gt;&lt;BR /&gt;The result for a test account on a 10g XE install on my PC is an (amazing!)...&lt;BR /&gt;&lt;BR /&gt;SQL&amp;gt; @csv.sql&lt;BR /&gt;&lt;BR /&gt;TABLE_NAME                     Columns...&lt;BR /&gt;------------------------------ ----------------------------------------------------------------------------------------------------&lt;BR /&gt;-----------------------------------------------------------------------------------------------------------------------------------&lt;BR /&gt;-----------------------------------------------------------------------------------------------------------------------------------&lt;BR /&gt;-----------------------------------------------------------------------------------------------------------------------------------&lt;BR /&gt;---&lt;BR /&gt;BIN$UTvAkVYzQ0WadVJQMNzvGg==$0 C,B,A&lt;BR /&gt;MY_PLAN_TABLE                  FILTER_PREDICATES,ACCESS_PREDICATES,TEMP_SPACE,IO_COST,CPU_COST,DISTRIBUTION,OTHER,PARTITION_ID,PART&lt;BR /&gt;TION_STOP,PARTITION_START,OTHER_TAG,BYTES,CARDINALITY,COST,POSITION,PARENT_ID,ID,SEARCH_COLUMNS,OPTIMIZER,OBJECT_TYPE,OBJECT_INSTAN&lt;BR /&gt;E,OBJECT_NAME,OBJECT_OWNER,OBJECT_NODE,OPTIONS,OPERATION,REMARKS,TIMESTAMP,STATEMENT_ID&lt;BR /&gt;TEST                           TEST&lt;BR /&gt;VT_FEATURE                     DESCRIPTION,FEATURE&lt;BR /&gt;VT_FEATURE_BASE                DESCRIPTION,FEATURE&lt;BR /&gt;&lt;BR /&gt;x rows selected.&lt;BR /&gt;&lt;BR /&gt;SQL&amp;gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;For specific tables, change that "from user_tab_columns" in the middle to "from user_tab_columns where table_name like '%....%'"&lt;BR /&gt;&lt;BR /&gt;Regards,&lt;BR /&gt;Hein van den Heuvel&lt;BR /&gt;HvdH Performance Consulting&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Tue, 20 Feb 2007 22:43:09 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/horizontal-output-from-oracle-sql/m-p/3948145#M760879</guid>
      <dc:creator>Hein van den Heuvel</dc:creator>
      <dc:date>2007-02-20T22:43:09Z</dc:date>
    </item>
    <item>
      <title>Re: Horizontal Output from ORACLE SQL</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/horizontal-output-from-oracle-sql/m-p/3948146#M760880</link>
      <description>Hi,&lt;BR /&gt;Why dont you connect to Oracle through MS EXCEL. This will help you.&lt;BR /&gt;&lt;BR /&gt;You should have system admin privileges.&lt;BR /&gt;Connect to ODBC.&lt;BR /&gt;Choose system DSN&lt;BR /&gt;Click on ADD button&lt;BR /&gt;Choose Oracle datasource&lt;BR /&gt;enter the required entries.&lt;BR /&gt;and test connection.&lt;BR /&gt;Go to excel&lt;BR /&gt;Choose Data--&amp;gt;Query database.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;I think this will help you.&lt;BR /&gt;&lt;BR /&gt;Regards&lt;BR /&gt;Subodh</description>
      <pubDate>Wed, 21 Feb 2007 01:29:03 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/horizontal-output-from-oracle-sql/m-p/3948146#M760880</guid>
      <dc:creator>Subodh deshpande_1</dc:creator>
      <dc:date>2007-02-21T01:29:03Z</dc:date>
    </item>
  </channel>
</rss>

