Preventing sqlplus truncation of column names, without individual column formatting

前端 未结 6 2323
感情败类
感情败类 2021-02-09 13:13

By default sqlplus truncates column names to the length of the underlying data type. Many of the column names in our database are prefixed by the table name, and therefore look

6条回答
  •  余生分开走
    2021-02-09 13:34

    None of the proposed solutions work to show the original column names, so I'm not sure why people are voting them up... I do have a "hack" that works for the original request, but I really don't like it... That is you actually append or prefix a string onto the query for each column so they are always long enough for the column heading. If you are in an HTML mode, as the poster is, there is little harm by a bit of extra white spacing... It will of course slow down your query abit...

    e.g.

    SET ECHO OFF
    SET PAGESIZE 32766
    SET LINESIZE 32766
    SET NUMW 20
    SET VERIFY OFF
    SET TERM OFF
    SET UNDERLINE OFF
    SET MARKUP HTML ON
    SET PREFORMAT ON
    SET WORD_WRAP ON
    SET WRAP ON
    SET ENTMAP ON
    spool '/tmp/Example.html'
    select 
       (s.ID||'                  ') AS ID,
       (s.ORDER_ID||'                  ') AS ORDER_ID,
       (s.ORDER_NUMBER||'                  ') AS ORDER_NUMBER,
       (s.CONTRACT_ID||'                  ') AS CONTRACT_ID,
       (s.CONTRACT_NUMBER||'                  ') AS CONTRACT_NUMBER,
       (s.CONTRACT_START_DATE||'                  ') AS CONTRACT_START_DATE,
       (s.CONTRACT_END_DATE||'                  ') AS CONTRACT_END_DATE,
       (s.CURRENCY_ISO_CODE||'                  ') AS CURRENCY_ISO_CODE,
    from Example s
    order  by s.order_number, s.contract_number;
    spool off;
    

    Of course you could write a stored procedure to do something better, but really it seems like overkill for this simple scenario.

    This still does not meet the original posters request either. In that it requires manually listing on the columns and not using select *. But at least it is solution that works when you are willing to detail out the fields.

    However, since there really is no problem having too long of fields in HTML, there is an rather simple way to fix Chris's solution to work it this example. That is just pick a use the maximum value oracle will allow. Sadly this still won't really work for EVERY field of every table, unless you explicitly add formatting for every data type. This solution also won't work for joins, since different tables can use the same column name but a different datatype.

    SET ECHO OFF
    SET TERMOUT OFF
    SET FEEDBACK OFF
    SET PAGESIZE 32766
    SET LINESIZE 32766
    SET MARKUP HTML OFF
    SET HEADING OFF
    
    spool /tmp/columns_EXAMPLE.sql
    select 'column ' || column_name || ' format A32766' 
    from all_tab_cols
    where data_type = 'VARCHAR2' and table_name = 'EXAMPLE'
    /
    spool off
    
    SET HEADING ON
    SET NUMW 40
    SET VERIFY OFF
    SET TERM OFF
    SET UNDERLINE OFF
    SET MARKUP HTML ON
    SET PREFORMAT ON
    SET WORD_WRAP ON
    SET WRAP ON
    SET ENTMAP ON
    @/tmp/columns_EXAMPLE.sql
    spool '/tmp/Example.html'
    select *
    from Example s
    order  by s.order_number, s.contract_number;
    spool off;
    

提交回复
热议问题