Search this blog ...

Friday, July 9, 2010

View ORACLE SQL query results with columns rendered vertically

Below is a script I hacked together from various sources to render ORACLE SQL query results in a vertical fashion (column are presented vertically rather than horizontally).

Tested with Oracle Database 11g Enterprise 11.2.0.1.0

:-

REM NAME: vselect.sql - Replacement by M Shannon.
prompt
accept tname prompt "Enter the table or view you wish to display vertically: "
prompt
prompt Enter the "WHERE" clause(s)...
prompt - if there is no "WHERE" clause, press [Enter].
prompt - do not include the word, "WHERE"; just specify syntax beyond "WHERE".
prompt - do not use single quotes for literals; use double quotes (") to enclose literals.
prompt
accept where prompt '=> '
prompt
prompt Enter the "ORDER BY" clause...
prompt - if there is no "ORDER BY" clause, press [Enter].
prompt - do not include the words, "ORDER BY"; just specify syntax beyond "ORDER BY"
prompt - do not use single quotes for literals; use double quotes (") to enclose literals.
prompt
accept orderby prompt '=> '
prompt

set termout on
set serveroutput on

declare
  l_where_clause    varchar2(500);
  l_orderby_clause  varchar2(500);
  l_cur             number;
  l_dtbl            dbms_sql.desc_tab;
  l_cnt             number;
  l_status          number;
  l_val             varchar2(200);
  double_quote      char(1) := '"';
  two_single_quotes char(1) := chr(39);
begin
  if length('&where') > 0 then
    l_where_clause := 'WHERE ' || replace(ltrim(rtrim('&where')),double_quote,two_single_quotes);
  else
    l_where_clause := null;
  end if;

  if length('&orderby') > 0 then
    l_orderby_clause := 'ORDER BY '|| replace(ltrim(rtrim('&orderby')),double_quote,two_single_quotes);
  else
    l_orderby_clause    := null;
  end if;

  l_cur := dbms_sql.open_cursor;
  dbms_sql.parse(l_cur,'select * from &tname '|| l_where_clause ||' '|| l_orderby_clause,dbms_sql.native);
  dbms_sql.describe_columns(l_cur,l_cnt,l_dtbl);
  for i in 1..l_cnt loop
    dbms_sql.define_column(l_cur,i,l_val,30);
  end loop;

  l_status := dbms_sql.execute(l_cur);

  while ( dbms_sql.fetch_rows(l_cur) > 0 ) loop
    dbms_output.put_line(lpad('=',80,'='));
    for i in 1..l_cnt loop
      dbms_sql.column_value(l_cur,i,l_val);
      dbms_output.put_line(rpad(l_dtbl(i).col_name,30) ||' --> '||l_val);
    end loop;
  end loop;
  dbms_sql.close_cursor(l_cur);
end;
/

Sample output:

SQL> @ /home/mshannon/other/scripts/database/vselect

Enter the table or view you wish to display vertically: dba_users

Enter the "WHERE" clause(s)...
- if there is no "WHERE" clause, press [Enter].
- do not include the word, "WHERE"; just specify syntax beyond "WHERE".
- do not use single quotes for literals; use double quotes (") to enclose literals.

=> username like "SY%"

Enter the "ORDER BY" clause...
- if there is no "ORDER BY" clause, press [Enter].
- do not include the words, "ORDER BY"; just specify syntax beyond "ORDER BY"
- do not use single quotes for literals; use double quotes (") to enclose literals.

=> username asc

...

================================================================================
USERNAME                       --> SYS
USER_ID                        --> 0
PASSWORD                       -->
ACCOUNT_STATUS                 --> OPEN
LOCK_DATE                      -->
EXPIRY_DATE                    --> 24-AUG-10
DEFAULT_TABLESPACE             --> SYSTEM
TEMPORARY_TABLESPACE           --> TEMP
CREATED                        --> 13-AUG-09
PROFILE                        --> DEFAULT
INITIAL_RSRC_CONSUMER_GROUP    --> SYS_GROUP
EXTERNAL_NAME                  -->
PASSWORD_VERSIONS              --> 10G 11G
EDITIONS_ENABLED               --> N
AUTHENTICATION_TYPE            --> PASSWORD
================================================================================
USERNAME                       --> SYSMAN
USER_ID                        --> 72
PASSWORD                       -->
ACCOUNT_STATUS                 --> OPEN
LOCK_DATE                      -->
EXPIRY_DATE                    --> 24-AUG-10
DEFAULT_TABLESPACE             --> SYSAUX
TEMPORARY_TABLESPACE           --> TEMP
CREATED                        --> 13-AUG-09
PROFILE                        --> DEFAULT
INITIAL_RSRC_CONSUMER_GROUP    --> DEFAULT_CONSUMER_GROUP
EXTERNAL_NAME                  -->
PASSWORD_VERSIONS              --> 10G 11G
EDITIONS_ENABLED               --> N
AUTHENTICATION_TYPE            --> PASSWORD
================================================================================
USERNAME                       --> SYSTEM
USER_ID                        --> 5
PASSWORD                       -->
ACCOUNT_STATUS                 --> OPEN
LOCK_DATE                      -->
EXPIRY_DATE                    --> 24-AUG-10
DEFAULT_TABLESPACE             --> SYSTEM
TEMPORARY_TABLESPACE           --> TEMP
CREATED                        --> 13-AUG-09
PROFILE                        --> DEFAULT
INITIAL_RSRC_CONSUMER_GROUP    --> SYS_GROUP
EXTERNAL_NAME                  -->
PASSWORD_VERSIONS              --> 10G 11G
EDITIONS_ENABLED               --> N
AUTHENTICATION_TYPE            --> PASSWORD

PL/SQL procedure successfully completed.

No comments:

Post a Comment