Preventing sqlplus truncation of column names, without individual column formatting

前端 未结 6 2326
感情败类
感情败类 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:28

    This should provide some reasonable formatting. You are, of course, free to substitute your own preferences for the maximum width of char columns, and what to do with LONG, RAW and LOB columns.

    SELECT 'COLUMN ' || column_name || ' FORMAT ' ||
           CASE
              WHEN data_type = 'DATE' THEN
               'A9'
              WHEN data_type LIKE '%CHAR%' THEN
               'A' ||
               TRIM(TO_CHAR(LEAST(GREATEST(LENGTH(column_name),
                            data_length), 40))) ||
               CASE
                  WHEN data_length > 40 THEN
                   ' TRUNC'
                  ELSE
                   NULL
               END
              WHEN data_type = 'NUMBER' THEN
               LPAD('0', GREATEST(LENGTH(column_name),
               NVL(data_precision, data_length)), '9') ||
               DECODE(data_scale, 0, NULL, NULL, NULL, '.' ||
               LPAD('0', data_scale, '0'))
              WHEN data_type IN ('RAW', 'LONG') THEN
               'A1 NOPRINT'
              WHEN data_type LIKE '%LOB' THEN
               'A1 NOPRINT'
              ELSE
               'A' || TRIM(TO_CHAR(GREATEST(LENGTH(column_name), data_length)))
           END AS format_cols
      FROM dba_tab_columns
     WHERE owner = 'SYS'
       AND table_name = 'DBA_TAB_COLUMNS';
    

提交回复
热议问题