0

我被要求创建一个基本上与 describe 命令执行相同操作的脚本。我已经想出了如何编写脚本并获得我想要的输出,但是任务的第二部分是“复制描述命令的格式”我很难过,真的有办法简单地复制那种格式吗?

4

1 回答 1

1

假设您的查询类似于(请注意,我不会费心处理每种不同的数据类型)

select column_name "Name",
       (case when nullable = 'N'
             then 'NOT NULL'
             else null
         end) "Null?",
       (case when data_type = 'DATE'
             then data_type
             when data_type = 'NUMBER' and data_scale > 0
             then data_type || '(' || data_precision || ',' || data_scale || ')'
             when data_type = 'NUMBER' and data_scale = 0
             then data_type || '(' || data_precision || ')'
             when data_type = 'VARCHAR2'
             then data_type || '(' || data_length || ')'
        end) "Type"
  from dba_tab_cols
 where table_name = 'EMP'
 order by column_id

您可以发出一些 SQL*Plus 命令来格式化输出

SQL> column "Nmae" format a30;
SQL> column "Null?" format a8;
SQL> column "Type" format a30;

然后查询的输出将与DESCRIBE命令的输出匹配

SQL> ed
Wrote file afiedt.buf

  1  select column_name "Name",
  2         (case when nullable = 'N'
  3               then 'NOT NULL'
  4               else null
  5           end) "Null?",
  6         (case when data_type = 'DATE'
  7               then data_type
  8               when data_type = 'NUMBER' and data_scale > 0
  9               then data_type || '(' || data_precision || ',' || data_scale || ')'
 10               when data_type = 'NUMBER' and data_scale = 0
 11               then data_type || '(' || data_precision || ')'
 12               when data_type = 'VARCHAR2'
 13               then data_type || '(' || data_length || ')'
 14          end) "Type"
 15    from dba_tab_cols
 16   where table_name = 'EMP'
 17*  order by column_id
SQL> /

Name                           Null?    Type
------------------------------ -------- ------------------------------
EMPNO                          NOT NULL NUMBER(4)
ENAME                                   VARCHAR2(10)
JOB                                     VARCHAR2(9)
MGR                                     NUMBER(4)
HIREDATE                                DATE
SAL                                     NUMBER(7,2)
COMM                                    NUMBER(7,2)
DEPTNO                                  NUMBER(2)

8 rows selected.
于 2012-11-13T19:56:33.033 回答